最近在學習Python,在使用Tkinter做圖形界面時遇到了幾個小問題,網上查了一下,在Python2.x導入的是Tkinter,Python3則是tkinter。而且導入的simpledialog和message也不一樣。具體是這樣的:Python2.x
1 from Tkinter import * 2 import tkSimpleDialog as dl 3 import tkMessageBox as mb
Python3
1 from tkinter import * 2 import tkinter.simpledialog as dl 3 import tkinter.messagebox as mb
一個猜數字的小游戲
1 #Python 2.7 2 from Tkinter import * 3 import tkSimpleDialog as dl 4 import tkMessageBox as mb 5 root=Tk() 6 w=Label(root,text='Guess Number Game') 7 w.pack() 8 mb.showinfo('Welcome','Welcome to Guess Number Game') 9 number=66 10 while True: 11 guess=dl.askinteger('Number','what\'s your guess?') 12 if guess==number: 13 output='Bingo!you guessed it right,but you do not win any prizes!' 14 mb.showinfo('hahah',output) 15 break 16 elif guess>number: 17 output='No,the number is a lower than that' 18 mb.showinfo('hahah',output) 19 else: 20 output='No,the number is a higher than that' 21 mb.showinfo('hahah',output) 22 print 'Done'