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



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

这基本上没问题,但我希望通过启用或不启用文本小部件来使其更具视觉吸引力,具体取决于下拉列表的值。基本上,我的想法是默认禁用文本小部件,如果用户从下拉列表中选择"是",则小部件变得可编辑。

我尝试这样做:

#! /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)。

我想问题出在主循环和检索选择的函数中,但我无法弄清楚如何解决这个问题。

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

提前感谢所有愿意给我一些提示的人。

您可以通过调用self.textChanges.config()并向其传递状态来启用/禁用文本小部件。这相当于修改您的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

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

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)

相关内容

  • 没有找到相关文章

最新更新