提问人:A2020 提问时间:11/10/2023 最后编辑:A2020 更新时间:11/10/2023 访问量:41
为动态生成的按钮创建“切换”函数
Create a "switched" function for dynamically generated buttons
问:
我正在创建一个工具来使用 tkinter 查看元数据。其用户将查看当前与多个数据集关联的元数据,并权衡元数据是否适合该数据集。每个数据集都必须有自己的窗口。我有一个粗略的布局,但我很难为 True/False 按钮编写函数。
import pandas as pd
from tkinter import *
from tkinter import ttk
def a(self,name):
print(name)
def btn_true(status, i):
if status == "off":
true_button.configure(relief=SUNKEN, bg="#D4D5D3")
false_button.configure(relief=RAISED, bg="#EDEDED")
status = "on"
def btn_false(status, i):
if status == "off":
true_button.configure(relief=RAISED, bg="#EDEDED")
false_button.configure(relief=SUNKEN, bg="#D4D5D3")
status = "on"
def submit_review():
print('')
df = pd.DataFrame({'dataset_name': ['people', 'places', 'things'],
'tags': ["['proper nouns', 'humans', 'UTF-8', 'NaN', '’', '']", "['proper nouns', 'country', 'geocode', 'alphanumeric']", "['jargon', 'websites', 'unicode']"],
'l_desc': ['Lorem ipsum dolor ','sit amet, consectetur ','adipiscing elit, sed '],
's_desc': ['Lorem','sit','adipiscing']})
DATASET_ROW = 0
tag_str = df.loc[DATASET_ROW,"tags"]
tag_str =tag_str.replace("'","")
tag_list = tag_str.strip('][').split(', ')
window = Tk()
window.title("Metadata Evaluation Tool")
window.geometry("700x400")
## Canvas & Scrollbar
outer_frame = Frame(window)
outer_frame.pack(fill=BOTH, expand=1)
canvas = Canvas(outer_frame)
canvas.grid_columnconfigure(0, weight=1, uniform="fred")
canvas.pack(side=LEFT, fill=BOTH, expand=1)
my_scrollbar = ttk.Scrollbar(outer_frame, orient=VERTICAL, command = canvas.yview)
my_scrollbar.pack(side=RIGHT, fill=Y)
canvas.configure(yscrollcommand = my_scrollbar.set)
canvas.grid_columnconfigure(0, weight=1, uniform="foo")
canvas.bind('<Configure>', lambda e: canvas.configure(scrollregion = canvas.bbox("all")))
## Place additional frames within the Canvas
metadata_frame = Frame(canvas)
canvas.create_window((0,0), window=metadata_frame, anchor=NW, )
# Page
current_dataset =Label(metadata_frame, font='Helvetica 12 bold', text=str(df.loc[DATASET_ROW,"dataset_name"]))
current_dataset.grid(row= 0, column=0, padx=20, pady=10, sticky=EW)
# Tags
tags_info_frame =LabelFrame(metadata_frame, text="Tags",)
tags_info_frame.grid(row= 1, column=0, padx=20, pady=10, sticky=EW)
for i in range(len(tag_list)):
iter_tag_label = Label(tags_info_frame, text=tag_list[i])
iter_tag_label.grid(row=i, column=0, sticky=W,)
status="on"
true_button = Button(tags_info_frame, text=" TRUE ", relief=SUNKEN, bg="#D4D5D3", command=lambda : btn_true(status=status, i=i))
true_button.grid(row=i, column=1)
status="off"
false_button = Button(tags_info_frame, text=" FALSE ", relief=RAISED, bg="#EDEDED", command=lambda : btn_false(status=status, i=i))
false_button.grid(row=i, column=2)
for widget in tags_info_frame.winfo_children():
widget.grid_configure(padx=10, pady=5)
# Descriptions
desc_info_frame =LabelFrame(metadata_frame, text="Description(s)")
desc_info_frame.grid(row= 2, column=0, padx=20, pady=10, sticky=EW)
l_desc_label = Label(desc_info_frame, text="Long Description")
l_desc_label.grid(row=0, column=0, sticky=W,)
l_desc_text = Label(desc_info_frame, text=str(df.loc[DATASET_ROW,"l_desc"]), wraplength=500, justify="left")
l_desc_text.grid(row=0, column=1, sticky=W,)
l_desc_entry = Text(desc_info_frame, height=5, width=20)
l_desc_entry.insert(END, str(df.loc[DATASET_ROW,"l_desc"]))
l_desc_entry.grid(row=1, column=1, sticky=EW,)
s_desc_label = Label(desc_info_frame, text="Short Description")
s_desc_label.grid(row=2, column=0, sticky=W,)
s_desc_text = Label(desc_info_frame, text=str(df.loc[DATASET_ROW,"s_desc"]), wraplength=500, justify="left")
s_desc_text.grid(row=2, column=1, sticky=W,)
s_desc_entry = Text(desc_info_frame, height=5, width=20)
s_desc_entry.insert(END, str(df.loc[DATASET_ROW,"s_desc"]))
s_desc_entry.grid(row=3, column=1, sticky=EW,)
for widget in desc_info_frame.winfo_children():
widget.grid_configure(padx=10, pady=5)
# Submit review
confirmation_frame =Frame(metadata_frame)
confirmation_frame.grid(row= 3, column=0, padx=20, pady=10, sticky=SE)
button = Button(confirmation_frame, text=" Submit ", command= submit_review)
button.grid(row=0, column=0, sticky=SE, padx=20, pady=10)
window.mainloop()
应该发生什么加载窗口时,应打开所有 TRUE 按钮,并关闭所有 FALSE 按钮。单击已关闭的按钮应反转这些设置,但仅适用于该行中的按钮。单击已打开的按钮应该不执行任何操作。
会发生什么加载窗口时,不会打开或关闭任何按钮。单击任何按钮仅切换最后一行中的按钮。
更新
按钮默认为正确的设置,但所有单击(无论哪一行)仅影响最后一行按钮。
答:
0赞
acw1668
11/10/2023
#1
您错误地应用了链接问题中的答案。
实际上,您可以使用单个函数而不是两个函数:
def btn_toggle(on_button, off_button):
on_button.configure(relief=SUNKEN, bg="#D4D5D3")
off_button.configure(relief=RAISED, bg="#EDEDED")
...
for i in range(len(tag_list)):
iter_tag_label = Label(tags_info_frame, text=tag_list[i])
iter_tag_label.grid(row=i, column=0, sticky=W,)
true_button = Button(tags_info_frame, text=" TRUE ", relief=SUNKEN, bg="#D4D5D3")
true_button.grid(row=i, column=1)
false_button = Button(tags_info_frame, text=" FALSE ", relief=RAISED, bg="#EDEDED")
false_button.grid(row=i, column=2)
true_button["command"] = lambda tb=true_button, fb=false_button: btn_toggle(tb, fb)
false_button["command"] = lambda tb=true_button, fb=false_button: btn_toggle(fb, tb)
...
评论
Radiobutton