2015-09-11 55 views
-3

我正在使用mac OS X python。我現在正在使用GUI,並使用三個按鈕製作一個簡單的窗口。我試圖配置一些按鈕,讓他們做一些事情,但它不工作。誰能告訴我問題是什麼?到目前爲止,我有一個三個按鈕的小窗口。我寫的代碼:GUI運行不正常

win=Tk() 
f=Frame(win) 
b1=Button(f,text="one") 
b2=Button(f,text"two") 
f.pack() 
def but1() : print "Button one was pushed" 
b1.configure(command=but1) 

我得到錯誤信息無效的語法。

+1

你能展示你的整個代碼嗎? – ASCIIThenANSI

+0

哪條線有錯誤? – user996142

+0

win = Tk() b1 = Button(win,text =「One」) b2 = Button(win,text =「Two」) b1.pack() def but1():print「Button one has been推「 b1.configure(command = but1) – Annie

回答

0

下面的代碼似乎很適合我。

from Tkinter import Tk, Button 

win = Tk() 
b1 = Button(win, text="One") 
b2 = Button(win,text="Two") 
b1.pack() 
def but1(): 
    print "Button one has been pushed" 
b1.configure(command=but1) 
+0

實際上,Python將允許您按照書面運行代碼,儘管正確的縮進對於可讀性來說很重要。 – thecircus

0

,我看到你的代碼錯誤的唯一的事情是,你忘了,包括=當你定義b2。正確運行您寫的內容會引發語法錯誤。

from Tkinter import * 

win = Tk() 
f = Frame(win) 
b1 = Button(f, text="one") 
b2 = Button(f, text="two") # Don't forget the equals sign. 
f.pack() 
def but1(): 
    print "Button one was pushed" 
b1.configure(command=but1) 
1

你的程序需要調用root.mainloop()在最後一行。您還有問題,您沒有在按鈕上撥打packgrid。在將電話添加到mainloop()後,您只會看到任何空白窗口,直到您在按鈕上撥打packgrid