【问题标题】:Tkinter - Python 3.5Tkinter - Python 3.5
【发布时间】:2015-07-30 03:41:13
【问题描述】:

Python 编程第三版。 isbn 978-1-4354-5500-9

下面的代码对我不起作用,谁能帮帮我?

我对 Tkinter 和 GUI 非常陌生...任何建议或资源将不胜感激

谢谢,亚当

import Tkinter
from Tkinter import *

class Application(Frame):

    def __init__(self,master=None):
        super(Application, self).__init__(master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.myButton = Button(self, text='Button Label')
        self.myButton.grid()

root = Tkinter.Tk()

root.title('Frame w/ Button')
root.geometry('200x200')

app = Application(root)
root.mainloop()

【问题讨论】:

  • 有错误信息吗?如果可能,请提供完整的追溯。
  • 您是否将文件命名为“Tkinter.py”?

标签: python python-3.x tkinter


【解决方案1】:

在 Python 3.x 中,Tkinter 模块重命名为 tkinter

try:
    # Python 3.x
    from tkinter import *
except ImportError:
    # Python 2.x
    from Tkinter import *

class Application(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.myButton = Button(self, text='Button Label')
        self.myButton.grid()

root = Tk()

root.title('Frame w/ Button')
root.geometry('200x200')

app = Application(root)
root.mainloop()

UPDATE 更改了代码以在 Python 2.x、Python 3.x 中运行。

【讨论】:

  • 我的电脑只适用于“Tkinter”...出现“tkinter”错误
  • @ARW,你确定你使用的是 Python 3.x。 python -V 打印什么?
  • 版本 3.5.0a4 (3.5.0a4)​​span>
  • @ARW,你如何运行你的脚本?在命令行中,还是双击?
  • 这是我在 Python 3.5b4 + Ubuntu 15.04 的答案中得到的截图:i.imgur.com/YceIrEQ.png
【解决方案2】:

我找到了答案....感谢 cmets

class Application(Frame):

    def __init__(self,master=None):
        Frame.__init__(self,master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.myButton = Button(self, text='Button Label')
        self.myButton.grid()

    root = Tkinter.Tk()

    root.title('Frame w/ Button')
    root.geometry('200x200')

    app = Application(root)
    root.mainloop()

【讨论】:

    猜你喜欢
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 2018-04-22
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    相关资源
    最近更新 更多