鱼C论坛

 找回密码
 立即注册
查看: 2370|回复: 5

[已解决]Tkinter问题

[复制链接]
发表于 2016-8-2 09:54:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请问一下大家,我用Tkinter编写GUI程序时为什么一直提示NameError:name 'Frame' is not defined?具体代码如下,希望大神告知一下
#!/usr/bin/python
# -*- coding: UTF-8 -*-

from Tkinter import *
import tkMessageBox

class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

    def createWidgets(self):
        self.nameInput = Entry(self)
        self.nameInput.pack()
        self.alertButton = Button(self, text='Hello', command=self.hello)
        self.alertButton.pack()

    def hello(self):
        name = self.nameInput.get() or 'world'
        tkMessageBox.showinfo('Message', 'Hello, %s' % name)

app = Application()
# 设置窗口标题:
app.master.title('Hello Worl')
# 主消息循环:
app.mainloop()
最佳答案
2016-8-2 10:54:57
tkMessageBox 也改为  messagebox
  1. from tkinter import *

  2. class Application(Frame):
  3.     def __init__(self, master=None):
  4.         Frame.__init__(self, master)
  5.         self.pack()
  6.         self.createWidgets()

  7.     def createWidgets(self):
  8.         self.nameInput = Entry(self)
  9.         self.nameInput.pack()
  10.         self.alertButton = Button(self, text='Hello', command=self.hello)
  11.         self.alertButton.pack()

  12.     def hello(self):
  13.         name = self.nameInput.get() or 'world'
  14.         messagebox.showinfo('Message', 'Hello, %s' % name)

  15. app = Application()
  16. # 设置窗口标题:
  17. app.master.title('Hello Worl')
  18. # 主消息循环:
  19. app.mainloop()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-8-2 10:48:06 | 显示全部楼层
Python 3.4.3
>>> from Tkinter import *
Traceback (most recent call last):
  File "D:/Python34/work/tk-test/x.py", line 1, in <module>
    from Tkinter import *
ImportError: No module named 'Tkinter'

--------
在 Python3中,Tkinter 改名为 tkinter
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-2 10:54:57 | 显示全部楼层    本楼为最佳答案   
tkMessageBox 也改为  messagebox
  1. from tkinter import *

  2. class Application(Frame):
  3.     def __init__(self, master=None):
  4.         Frame.__init__(self, master)
  5.         self.pack()
  6.         self.createWidgets()

  7.     def createWidgets(self):
  8.         self.nameInput = Entry(self)
  9.         self.nameInput.pack()
  10.         self.alertButton = Button(self, text='Hello', command=self.hello)
  11.         self.alertButton.pack()

  12.     def hello(self):
  13.         name = self.nameInput.get() or 'world'
  14.         messagebox.showinfo('Message', 'Hello, %s' % name)

  15. app = Application()
  16. # 设置窗口标题:
  17. app.master.title('Hello Worl')
  18. # 主消息循环:
  19. app.mainloop()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-8-2 11:15:31 | 显示全部楼层
SixPy 发表于 2016-8-2 10:54
tkMessageBox 也改为  messagebox

多谢,已经修改成功了,但是还想问下如果用python2.7的话代码应该怎么改呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-2 11:18:38 | 显示全部楼层
在 idle中按 F1
然后搜索 tkinter 就知道了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-8-2 12:37:14 | 显示全部楼层
SixPy 发表于 2016-8-2 11:18
在 idle中按 F1
然后搜索 tkinter 就知道了

多谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-18 11:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表