首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Tkinter (Python)创建用户界面

使用Tkinter (Python)创建用户界面
EN

Stack Overflow用户
提问于 2013-07-25 16:49:24
回答 2查看 1.2K关注 0票数 2

我看了一篇关于使用Tkinter的教程,并看到了以下代码:

代码语言:javascript
运行
复制
>>> from Tkinter import *
>>> win=Tk()

这应该会产生一个标题为Tk的盒子,而不是其他任何东西。但是,当我尝试使用此代码时,不会出现这样的框。我没有收到任何错误,所以我怀疑它是正常工作的。如果我在mac电脑上,是否还需要采取其他步骤呢?

代码语言:javascript
运行
复制
from Tkinter import *

root = Tk()

w = Label(root, text="Hello, world!")
w.pack()

root.mainloop()

然而,在指南中,这段代码会自动运行,它建议我使用$pythonhello1.py来运行这段代码,但它不起作用。知道为什么会这样吗?

但是,这个更大的块不起作用:

代码语言:javascript
运行
复制
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()部分中运行得很好。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-25 16:52:11

您是否在空闲状态下运行此代码?

尝试上面的代码在终端(而不是在空闲),然后它将按预期工作。

票数 3
EN

Stack Overflow用户

发布于 2014-11-11 07:44:52

因此,如果您想尝试在终端中运行它,您应该遵循以下步骤

注意:“我发现运行程序是一个终端,包含一个tkinter Gui,对我来说经常会崩溃,但是它可能对你有用。”

代码语言:javascript
运行
复制
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/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17864055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档