首页 > 解决方案 > 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()

它有组合框,但是一旦我选择一个选项(是或否),它就不会更新以下选项。请帮我解决这个问题,因为我不确定如何根据实时输入更新应用程序。一旦我单击按钮,我不希望它更新。这也只是我遇到问题的代码部分,请告知。

标签: pythontkinter

解决方案


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_stepfromclicked()并从组合框中删除绑定。

好的,根据您的要求,这里是更新的代码:

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


推荐阅读