我的第一个Python/Tkinter项目已经完成了1000行。是时候给我点提示了。 所以我想先禁用显示布局按钮(灰显)。加载文件后,显示布局处于活动状态。在
def ReadFile()
#Something Magical Happens
Layoutbutton.config(state='active')
def DisplayLayout()
#Draw Great gobs of stuff
def main()
global Layoutbutton
#setup root window yada yada yada
Layoutbutton=Button(root,text="Layout",command=DisplayLayout,underline=0)
Layoutbutton.place(relx=.5,rely=.85,anchor=CENTER)
root.bind("l",DisplayLayout)
root.bind('L',DisplayLayout)
Layoutbutton.config(state='disabled')
BrowesButton=Button(root,text="File",command=ReadFile,underline=0)
BrowesButton.place(relx=.75,rely=.85,anchor=CENTER)
root.bind("F",ReadFile)
root.bind('f',ReadFile)
if __name__ == '__main__':
root=Tk()
main()
这一切如期而至。DisplayLayout按钮一直显示,直到文件被读取。在
我质疑使用全局布局按钮。我有很多这样的事情发生。 在没有太多全局变量的情况下(因为没有更好的术语)兄弟窗口如何相互通信?在
使函数
ReadFile
接受参数:在}。将出现的
^{pr2}$main
内定义一个函数,用LayoutButton
调用{ReadFile
替换为新函数。在或者,您可以使用
lambda
代替函数:或者用类构造程序。在
通过将代码对象化,可以避免使用全局变量。例如,您的示例的不同实现可能是:
这可能是个人偏好,但我发现这样的GUI设计更直观,而且与大量使用全局变量的代码相比,跟踪不需要的行为更容易。通过这种方法,每一帧都保持对其父帧的引用,本质上使所有帧在需要更新时彼此访问。一个完整而相当愚蠢的例子可能是:
^{pr2}$相关问题 更多 >
编程相关推荐