2015-11-20 33 views

回答

2

只需使用command函數,只要點擊按鈕,它就會調用一個函數。

import tkinter 

tickets = 0 

def ticket(): 
    tickets = 1 
    print(tickets) 

def twotickets(): 
    tickets = 2 
    print(tickets) 

def threetickets(): 
    tickets = 3 
    print(tickets) 

window = tkinter.Tk() 

label = tkinter.Label(window, text="How many tickets would you like?") 
button = tkinter.Button(window, text="One ticket", command=ticket) 
button2 = tkinter.Button(window, text="Two tickets", command=twotickets) 
button3 = tkinter.Button(window, text="Three tickets", command=threetickets) 

label.pack() 
button.pack() 
button2.pack() 
button3.pack() 
window.mainloop() 

只要您將它們綁定到函數,如果您希望它們執行任何操作,就可以擁有儘可能多的這些按鈕。另外,請記住,如果您想使用此已更改的變量,請在全球範圍內將其用於您想要使用的變量。

相關問題