代码:
import sys
from tkinter import *
credit = 0
coins = 0
choice = 0
credit1 = 0
coins = 0
prices = [200,150,160,50,90]
item = 0
i = 0
temp=0
n=0
choice1 = 0
choice2 = 0
credit1 = 0
coins = 0
prices = [200,150,160,50,90]
item = 0
i = 0
temp=0
n=0
choice1 = 0
choice2 = 0
def insert():
insert = Tk()
insert.geometry("450x250")
iLabel = Label(insert, text="Enter coins.[Press Buttons]").grid(row=1, column=1)
tenbutton = Button(insert, text="10p").grid(row=2, column=1)
twentybutton = Button(insert, text="20p").grid(row=3, column=1)
fiftybutton = Button(insert, text="50p").grid(row=4, column=1)
poundbutton = Button(insert, text="£1").grid(row=5, column=1)
我正在创建一个模拟自动售货机的程序。 我如何告诉Python“检查”是否按下了按钮? 在伪代码中是:
if tenbutton is pressed:
Add 10p to credit
如果按下tenbutton,我将如何用Python编写?提前谢谢你。
您可以将
command
添加到TkinterButton
小部件中,该小部件将回调函数:见:http://effbot.org/tkinterbook/button.htm
很简单,定义一个在按下按钮后调用的函数。就像这样:
然后将此简单功能分配给按钮:
顺便说一句,您的代码要求在某个地方使用
class
。使用这么多globals通常是一种不好的做法。另一个问题是from tkinter import *
,它破坏了可读性。我建议import tkinter as tk
。相关问题 更多 >
编程相关推荐