python - Tkinter 小部件没有根据输入进行更新?
问题描述
我使用 tkinter 库在 python 中创建了一个小部件。它有一个组合框
from tkinter import *
from tkinter.ttk import *
import os
import time
import win32api
def f_step():
window=Tk()
style = Style()
style.configure('W.TButton', font =
('calibri', 12, 'bold'),
foreground = 'blue')
s = Style() # Creating style element
s.configure('Wild.TRadiobutton', # First argument is the name of style. Needs to end with: .TRadiobutton
background='bisque2', # Setting background to our specified color above
font = "calibri 10 bold") # You can define colors like this also
window.title('Tool tool')
window.geometry("700x300+10+10")
#window.geometry(side="top", fill="both", expand = True)
txt_1=StringVar()
lbl_1 = Label(window,text="Are result files same", background ="bisque2",font = "calibri 10 bold").place(x=10,y=40)
lbl_2 = ttk.Combobox(window,textvariable = txt_1, values = ["Yes","No"], background ="bisque2",font = "calibri 10 bold").place(x=275,y=40)
a1 = txt_1.get()
if a1 == "Yes":
txt_2=StringVar()
lbl_3 = Label(window,text="Working directory path", background ="bisque2",font = "calibri 10 bold").place(x=10,y=70)
txt_b = Entry(window,textvariable=txt_2,width=60).place(x=275,y=70)
txt_3=StringVar()
lbl_4 = Label(window,text="file name (without extenstion)", background ="bisque2",font = "calibri 10 bold").place(x=10,y=100)
txt_c = Entry(window,textvariable=txt_2,width=60).place(x=275,y=100)
elif a1 == "No":
txt_2=StringVar()
lbl_3 = Label(window,text="Working directory path", background ="bisque2",font = "calibri 10 bold").place(x=10,y=70)
txt_b = Entry(window,textvariable=txt_2,width=60).place(x=275,y=70)
txt_3=StringVar()
lbl_4 = Label(window,text="file1 name (without extenstion)", background ="bisque2",font = "calibri 10 bold").place(x=10,y=100)
txt_c = Entry(window,textvariable=txt_2,width=60).place(x=275,y=100)
txt_4=StringVar()
lbl_5 = Label(window,text="file2 name (without extenstion)", background ="bisque2",font = "calibri 10 bold").place(x=10,y=130)
txt_d = Entry(window,textvariable=txt_2,width=60).place(x=275,y=130)
btn_1 = Button(window, text="run",style = 'W.TButton',command=clicked)
btn_1.place(x=300,y=250)
window.configure(bg='bisque2')
window.resizable(0, 0)
window.mainloop()
def clicked():
a=1212
f_step()
它有组合框,但是一旦我选择一个选项(是或否),它就不会更新以下选项。请帮我解决这个问题,因为我不确定如何根据实时输入更新应用程序。一旦我单击按钮,我不希望它更新。这也只是我遇到问题的代码部分,请告知。
解决方案
您lbl_2.bind("<<ComboboxSelected>>", f_step)
还要注意的是,您必须在f_step
函数中添加一个参数,因为它是通过组合虚拟事件传递的。这将确保在选项更改时更新
试试这个:
from tkinter import *
from tkinter.ttk import *
import os
import time
import win32api
def f_step(event=None):
a1 = txt_1.get()
if a1 == "Yes":
txt_2=StringVar()
lbl_3 = Label(window,text="Working directory path", background ="bisque2",font = "calibri 10 bold").place(x=10,y=70)
txt_b = Entry(window,textvariable=txt_2,width=60).place(x=275,y=70)
txt_3=StringVar()
lbl_4 = Label(window,text="file name (without extenstion)", background ="bisque2",font = "calibri 10 bold").place(x=10,y=100)
txt_c = Entry(window,textvariable=txt_2,width=60).place(x=275,y=100)
elif a1 == "No":
txt_2=StringVar()
lbl_3 = Label(window,text="Working directory path", background ="bisque2",font = "calibri 10 bold").place(x=10,y=70)
txt_b = Entry(window,textvariable=txt_2,width=60).place(x=275,y=70)
txt_3=StringVar()
lbl_4 = Label(window,text="file1 name (without extenstion)", background ="bisque2",font = "calibri 10 bold").place(x=10,y=100)
txt_c = Entry(window,textvariable=txt_2,width=60).place(x=275,y=100)
txt_4=StringVar()
lbl_5 = Label(window,text="file2 name (without extenstion)", background ="bisque2",font = "calibri 10 bold").place(x=10,y=130)
txt_d = Entry(window,textvariable=txt_2,width=60).place(x=275,y=130)
def clicked():
a=1212
#f_step()
window=Tk()
style = Style()
style.configure('W.TButton', font =
('calibri', 12, 'bold'),
foreground = 'blue')
s = Style() # Creating style element
s.configure('Wild.TRadiobutton', # First argument is the name of style. Needs to end with: .TRadiobutton
background='bisque2', # Setting background to our specified color above
font = "calibri 10 bold") # You can define colors like this also
window.title('Tool tool')
window.geometry("700x300+10+10")
#window.geometry(side="top", fill="both", expand = True)
txt_1=StringVar()
lbl_1 = Label(window,text="Are result files same", background ="bisque2",font = "calibri 10 bold").place(x=10,y=40)
lbl_2 = Combobox(window,textvariable = txt_1, values = ["Yes","No"], background ="bisque2",font = "calibri 10 bold")
lbl_2.place(x=275,y=40)
#lbl_2.current(1)
lbl_2.bind("<<ComboboxSelected>>", f_step)
btn_1 = Button(window, text="run",style = 'W.TButton',command=clicked)
btn_1.place(x=300,y=250)
window.configure(bg='bisque2')
window.resizable(0, 0)
window.mainloop()
此外,如果您只想在单击运行时更新,您可以调用f_step
fromclicked()
并从组合框中删除绑定。
好的,根据您的要求,这里是更新的代码:
from tkinter import *
from tkinter.ttk import *
import os
import time
import win32api
def f_step(event=None):
global lbl_3
global txt_b
global lbl_4
global lbl_c
global lbl_5
global txt_d
hide()
a1 = txt_1.get()
txt_2=StringVar()
txt_3=StringVar()
lbl_3 = Label(window,text="Working directory path", background ="bisque2",font = "calibri 10 bold")
lbl_3.place(x=10,y=70)
txt_b = Entry(window,textvariable=txt_2,width=60)
txt_b.place(x=275,y=70)
txt_b = Entry(window,textvariable=txt_2,width=60)
txt_b.place(x=275,y=70)
lbl_4 = Label(window,text="file name (without extenstion)", background ="bisque2",font = "calibri 10 bold")
lbl_4.place(x=10,y=100)
txt_c = Entry(window,textvariable=txt_2,width=60)
txt_c.place(x=275,y=100)
if a1 == "No":
lbl_5 = Label(window,text="file2 name (without extenstion)", background ="bisque2",font = "calibri 10 bold")
lbl_5.place(x=10,y=130)
txt_d = Entry(window,textvariable=txt_2,width=60)
txt_d.place(x=275,y=130)
def hide():
#lbl_3.place_forget()
lbl_3.destroy()
txt_b.destroy()
lbl_4.destroy()
txt_c.destroy()
lbl_5.destroy()
txt_d.destroy()
def clicked():
a=1212
f_step()
window=Tk()
style = Style()
style.configure('W.TButton', font =
('calibri', 12, 'bold'),
foreground = 'blue')
s = Style() # Creating style element
s.configure('Wild.TRadiobutton', # First argument is the name of style. Needs to end with: .TRadiobutton
background='bisque2', # Setting background to our specified color above
font = "calibri 10 bold") # You can define colors like this also
window.title('Tool tool')
window.geometry("700x300+10+10")
lbl_3 = Label(window)
txt_b = Entry(window)
txt_b = Entry(window)
lbl_4 = Label(window)
txt_c = Entry(window)
lbl_5 = Label(window)
txt_d = Entry(window)
txt_1=StringVar()
lbl_1 = Label(window,text="Are result files same", background ="bisque2",font = "calibri 10 bold").place(x=10,y=40)
lbl_2 = Combobox(window,textvariable = txt_1, values = ["Yes","No"], background ="bisque2",font = "calibri 10 bold")
lbl_2.place(x=275,y=40)
#lbl_2.current(1)
lbl_2.bind("<<ComboboxSelected>>", f_step)
btn_1 = Button(window, text="run",style = 'W.TButton',command=clicked)
btn_1.place(x=300,y=250)
window.configure(bg='bisque2')
window.resizable(0, 0)
window.mainloop()
您可以使用widget.destroy()
一起删除小部件或widget.place_forget
暂时隐藏小部件。注意:如果您使用widget.place_forget
,则无需重新创建小部件,而是使用更改现有小部件widget.config
推荐阅读
- python - 我得到“元组”对象不可调用为错误
- python - Python:Insert 语句中单个 CSV 单元格中的多个值。收到缺少逗号的错误
- hyperlink - 为什么当我尝试以 Google 身份提取时,网站管理员显示错误?
- javascript - 更新现有对象的最有效方法(使用类似的对象)
- python - 在 python 中杀死 sudo 启动的子进程
- node.js - 在所有查询都运行后发送响应
- python - “元组”对象不支持项目分配 - 打开(索引)
- uilabel - UiLabel 文本从 ViewController Swift 中输出
- c# - 带有代码的 C# 内存地址扩展
- javascript - websockets - 从 websocket 服务器获取消息并将其发送到客户端