【发布时间】:2020-03-05 19:54:46
【问题描述】:
在用 C# 编程了几年之后,我正试图自学用 python 编码。到目前为止,我发现过渡非常困难,但希望通过一些坚持我最终会理解 python
我的问题:在尝试了 wxglade 和 wxformbuilder 之类的程序之后,我被这些程序为您提供了一个很好的独立 GUI 文件的方式所吸引,这样您就可以将所有功能放在一个单独的主文件中。但是,我想将相同的原理应用于 tkinter GUI,但我无法弄清楚,也找不到有关它的文档。
假设我有两个文件: GUI.py:
from tkinter import *
class BotGUI:
def __init__(self, master):
# ***** Creation of the frame for all items to be on *****
xWidth=800
yWidth=500
margin=100
outer_frame=Frame(master, width=xWidth, height=yWidth, bg='lightgray')
outer_frame.pack(ipadx=margin, ipady=margin)
button1 = Button(outer_frame, text = "Print something", command = printcommand)
button1.pack()
#Virtual event handlers
def printcommand(self, event):
event.Skip()
然后是一个名为 Main.py 的文件:
from tkinter import *
import GUI
class GUI_Functions(GUI.BotGUI):
def __init__(self,parent):
GUI.BotGUI.__init__(self, parent)
def printcommand(self, event):
print("The bind was successful!")
if __name__ == '__main__':
root = Tk()
GUI_Frame = GUI.BotGUI(root)
root.mainloop()
以上代码导致错误:
NameError: global name 'printcommand' is not defined
我只是想不通如何让它为 tkinter GUI 工作。希望有人能帮帮我!
(使用 Visual Studio 在 C# 中创建 GUI 要容易得多:( )
【问题讨论】:
-
您不应该在问题帖子中提供您的问题的答案。如果您的问题得到解决,请选择解决问题的答案,或者使用最底部的文本框提供您自己的答案作为答案。
-
感谢您的建议 Nae,我是参与堆栈溢出的新手(到目前为止,只是一直在汲取:p)。我已经从我的第一个帖子中删除了答案,并在页面底部找到了答案按钮。
标签: python user-interface tkinter