1

作为我平时工作的一个附带项目,我被要求创建一个表格来收集信息。现在脚本已经成型,基本上可以满足我的所有需求,但我想添加一个额外的功能。基本上,我有一些由 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)。

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

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

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

4

1 回答 1

0

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)
于 2016-03-01T14:10:30.570 回答