python tkinter:使用下拉菜单启用文本字段

python tkinter: use a drop-down menu to enable a text field

作为我日常工作的副业,我被要求创建一个表格来收集信息。 现在脚本已经成形,基本上可以完成我需要的所有工作,但我想添加一个额外的功能。 基本上,我有一些由 3 个小部件组成的字段:一个带有一些问题的标签,一个带有 "Yes"/"No" 列表到 select 的下拉菜单和一个可选的文本小部件,其中用户可以提供额外信息(通常如果下拉列表中的 selection 是 "Yes") 目前,文本小部件始终处于活动状态(即用户可以编写我想要的所有内容),并且只是在 post-处理阶段(由脚本制作),我根据 drop 的值收集其内容-向下 selection.

这基本上没问题,但我希望根据下拉菜单的值启用或不启用文本小部件,从而使它更具视觉吸引力。基本上,我的想法是默认禁用文本小部件,如果下拉列表中的用户 select "Yes",则小部件变为可编辑。

我试过这样做:

#! /bin/env python


from Tkinter import *
#

my_change = "No"


stateOpts = ['normal','disabled']


win_width = 610
win_height =866

# My frame for form
class simpleform_ap(Tk):

    #my_sel = ""
    global my_change

    # constructor
    def __init__(self,parent):
        Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()
    #

    def initialize(self):
        #
        # Default Window Size
        self.geometry(str(win_width) + "x" + str(win_height))
        #
        self.grid_columnconfigure(0,weight=4)
        self.grid_rowconfigure(0,weight=1)
        #
        # Create a Canvas
        self.canvas=Canvas(self.parent)
        self.canvas.grid(row=0,column=0,sticky='nsew')
        #
        # Add a Frame inside my canvas
        self.frame1 = Frame(self.canvas)
        #
        # Create canvas window
        self.canvas.create_window(0,0,window=self.frame1,anchor='nw')
        #
        curr_row = 1
        text_height = 8
        text_width = 5*text_height
        # Label
        self.labelVariableChanges = StringVar()  # Label variable
        labelChanges=Label(self.frame1,textvariable=self.labelVariableChanges,
                 anchor="nw",
                 fg="Black")
        labelChanges.grid(column=0,row=curr_row,columnspan=1,sticky='EW')
        self.labelVariableChanges.set("Do your selection: ")
        # Selection
        optionList = ["Yes","No"]
        self.dropVarChanges=StringVar()
        self.dropVarChanges.set("Select")
        self.dropMenuChanges = OptionMenu(self.frame1 ,self.dropVarChanges,
                                            *optionList, command=self.get_Change_selection)
        self.dropMenuChanges.grid(column=2,row=curr_row)
        curr_row = curr_row+1
        # Text
        self.textStateChanges = stateOpts[1]
        self.TextVarChanges=StringVar()
        #
        self.textChanges=Text(self.frame1,state=self.textStateChanges,
                            height = text_height, width = text_width,
                            fg="black",bg="white") # ,textvariable=self.myTextVar)
        #
        self.textChanges.grid(column=1, row=curr_row+3, columnspan=2, sticky='EW')
        self.textChanges.insert('1.0',"If Yes, add changes here!")
        #
        #
        # Update Geometry
        self.update()
        self.geometry(self.geometry() )
        self.frame1.update_idletasks()
        self.canvas.config(scrollregion=(0,0,self.frame1.winfo_width(),self.frame1.winfo_height()))
        #
        # end initialize()


    def get_Change_selection(self,value):
        global my_change
        #
        my_change = value
        if value == 'Yes':
        #
            self.textStateChanges = stateOpts[0]
            #
        self.dropVarChanges.set(value)
        return


def release_block(argv):
    # Create Form
    form = simpleform_ap(None)
    form.title('Release Information')
    #
    form.mainloop()

    print "Done"
    #



if __name__ == "__main__":
    release_block(sys.argv)

但我失败了(基本上我是 TKinter 的 n00b)。

我猜问题出在主循环和检索 selection 的函数中,但我不知道如何解决这个问题。

有人知道如何解决这个问题吗?

在此先感谢所有愿意给我一些提示的人。

您可以通过调用 self.textChanges.config() 并将状态传递给它来 enable/disable 文本小部件。这相当于将您的 get_Change_selection 修改为:

def get_Change_selection(self,value):
    global my_change
    my_change = value
    if value == 'Yes':
        self.textChanges.config(state=stateOpts[0])
    else:
        self.textChanges.config(state=stateOpts[1])
    self.dropVarChanges.set(value)

    return

如果您想默认禁用该字段,请在构造函数中添加对配置的调用以禁用它。进行这些更改后,您的代码将如下所示:

from Tkinter import *
#

my_change = "No"


stateOpts = ['normal', 'disabled']


win_width = 610
win_height =866

# My frame for form
class simpleform_ap(Tk):

    #my_sel = ""
    global my_change

    # constructor
    def __init__(self,parent):
        Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()
    #

    def initialize(self):
        #
        # Default Window Size
        self.geometry(str(win_width) + "x" + str(win_height))
        #
        self.grid_columnconfigure(0,weight=4)
        self.grid_rowconfigure(0,weight=1)
        #
        # Create a Canvas
        self.canvas=Canvas(self.parent)
        self.canvas.grid(row=0,column=0,sticky='nsew')
        #
        # Add a Frame inside my canvas
        self.frame1 = Frame(self.canvas)
        #
        # Create canvas window
        self.canvas.create_window(0,0,window=self.frame1,anchor='nw')
        #
        curr_row = 1
        text_height = 8
        text_width = 5*text_height
        # Label
        self.labelVariableChanges = StringVar()  # Label variable
        labelChanges=Label(self.frame1,textvariable=self.labelVariableChanges,
                           anchor="nw",
                           fg="Black")
        labelChanges.grid(column=0,row=curr_row,columnspan=1,sticky='EW')
        self.labelVariableChanges.set("Do your selection: ")
        # Selection
        optionList = ["Yes","No"]
        self.dropVarChanges=StringVar()
        self.dropVarChanges.set("Select")
        self.dropMenuChanges = OptionMenu(self.frame1 ,self.dropVarChanges,
                                          *optionList, command=self.get_Change_selection)
        self.dropMenuChanges.grid(column=2,row=curr_row)
        curr_row = curr_row+1
        # Text
        self.textStateChanges = stateOpts[1]
        self.TextVarChanges=StringVar()
        #
        self.textChanges=Text(self.frame1,state=self.textStateChanges,
                              height = text_height, width = text_width,
                              fg="black",bg="white") # ,textvariable=self.myTextVar)
        #
        self.textChanges.grid(column=1, row=curr_row+3, columnspan=2, sticky='EW')
        self.textChanges.insert('1.0',"If Yes, add changes here!")
        self.textChanges.config(state=stateOpts[1])
        #
        #
        # Update Geometry
        self.update()
        self.geometry(self.geometry() )
        self.frame1.update_idletasks()
        self.canvas.config(scrollregion=(0,0,self.frame1.winfo_width(),self.frame1.winfo_height()))
        #
        # end initialize()


    def get_Change_selection(self,value):
        global my_change
        my_change = value
        if value == 'Yes':
            self.textChanges.config(state=stateOpts[0])
        else:
            self.textChanges.config(state=stateOpts[1])
        self.dropVarChanges.set(value)

        return


def release_block(argv):
    # Create Form
    form = simpleform_ap(None)
    form.title('Release Information')
    #
    form.mainloop()

    print("Done")
    #



if __name__ == "__main__":
    release_block(sys.argv)