Python图形界面Tk

时间:2023-03-09 07:49:17
Python图形界面Tk

最近在学习Python,在使用Tkinter做图形界面时遇到了几个小问题,网上查了一下,在Python2.x导入的是Tkinter,Python3则是tkinter。而且导入的simpledialog和message也不一样。具体是这样的:Python2.x

 from Tkinter import *
import tkSimpleDialog as dl
import tkMessageBox as mb

Python3

 from tkinter import *
import tkinter.simpledialog as dl
import tkinter.messagebox as mb

一个猜数字的小游戏

 #Python 2.7
from Tkinter import *
import tkSimpleDialog as dl
import tkMessageBox as mb
root=Tk()
w=Label(root,text='Guess Number Game')
w.pack()
mb.showinfo('Welcome','Welcome to Guess Number Game')
number=66
while True:
guess=dl.askinteger('Number','what\'s your guess?')
if guess==number:
output='Bingo!you guessed it right,but you do not win any prizes!'
mb.showinfo('hahah',output)
break
elif guess>number:
output='No,the number is a lower than that'
mb.showinfo('hahah',output)
else:
output='No,the number is a higher than that'
mb.showinfo('hahah',output)
print 'Done'