我看了一篇关于使用Tkinter的教程,并看到了以下代码:
>>> from Tkinter import *
>>> win=Tk()
这应该会产生一个标题为Tk的盒子,而不是其他任何东西。但是,当我尝试使用此代码时,不会出现这样的框。我没有收到任何错误,所以我怀疑它是正常工作的。如果我在mac电脑上,是否还需要采取其他步骤呢?
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
然而,在指南中,这段代码会自动运行,它建议我使用$pythonhello1.py来运行这段代码,但它不起作用。知道为什么会这样吗?
但是,这个更大的块不起作用:
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button = Button(
frame, text="QUIT", fg="red", command=frame.quit
)
self.button.pack(side=LEFT)
self.hi_there = Button(frame, text="Hello", command=self.say_hi)
self.hi_there.pack(side=LEFT)
def say_hi(self):
print "hi there, everyone!"
root = Tk()
app = App(root)
root.mainloop()
root.destroy() # optional; see description below
这个问题似乎与fine循环有关,但我感到困惑,因为与此同时,早期的块在root.mainloop()部分中运行得很好。
发布于 2013-07-25 16:52:11
您是否在空闲状态下运行此代码?
尝试上面的代码在终端(而不是在空闲),然后它将按预期工作。
发布于 2014-11-11 07:44:52
因此,如果您想尝试在终端中运行它,您应该遵循以下步骤
注意:“我发现运行程序是一个终端,包含一个tkinter Gui,对我来说经常会崩溃,但是它可能对你有用。”
1st - Open Terminal
2nd - Type 'python3.4' then press space bar once
3rd - Open a Finder window
4th - Go to where you saved your python file in the Finder window
5th - Once you have located the file in Finder, drag the file into the Terminal window
6th - Press enter, and enjoy your python program.
另一个注意事项--“听起来你需要一个更好的Python,您应该尝试一下PyCharm --这是一个很棒的Python,您可以在包含tkinter的东西中编写和运行python程序。”
您可以在这里下载PyCharm,https://www.jetbrains.com/pycharm/
https://stackoverflow.com/questions/17864055
复制相似问题