当我试图运行基于示例Tkinter的程序时,什么都不会发生。我对Pydev还不熟悉,但我发现这很不寻常。我有一个包含代码的Main.py文件,我试图简单地运行这个模块,但没有成功。
我只是从本参考复制/粘贴,
# Main.py
import tkinter as tk;
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.create_widgets()
def create_widgets(self):
self.hi_there = tk.Button(self)
self.hi_there["text"] = "Hello World\n(click me)"
self.hi_there["command"] = self.say_hi
self.hi_there.pack(side="top")
self.quit = tk.Button(self, text="QUIT", fg="red",
command=root.destroy)
self.quit.pack(side="bottom")
def say_hi(self):
print("hi there, everyone!")
root = tk.Tk()
app = Application(master=root)
app.mainloop()运行该模块的唯一结果是一个专用于Main.py的空Liclipse控制台。我也尝试了其他网站的其他例子,没有运气。此外,如果这件事重要的话,我现在也在MacOS上。
发布于 2017-04-20 15:06:35
你确定你有正确的一切(工作目录,python路径.)配置在利格利普?我刚刚尝试了一个新的安装,在配置Liclipse以运行Python3.6中的当前项目之后,在选择主项目文件作为源代码之后,它运行并显示一个窗口,并按预期的按钮显示,它还会将文本打印到控制台。
而且,用这种方式初始化按钮也不太"pythonic“。我宁愿这样建议:
# Main.py
import tkinter as tk;
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.create_widgets()
def create_widgets(self):
mytext = "Hello World\n(click me)"
self.hi_there = tk.Button(self, text=mytext, command=self.say_hi)
self.hi_there.pack(side="top")
self.quit = tk.Button(self, text="QUIT", fg="red", command=root.destroy)
self.quit.pack(side="bottom")
def say_hi(self):
print("hi there, everyone!")
root = tk.Tk()
app = Application(master=root)
app.mainloop()代码看起来更加可读性更强,并以同样的方式工作。当您开始扩展您的界面时,它将有许多行长,并且通过在每个按钮上减少两行,您可以使它更短。我一直在用1500行代码编写一个tkinter应用程序,那时我向自己承诺,我将努力学习如何使它更有条理和更简短;)
https://stackoverflow.com/questions/43496504
复制相似问题