检查是否在python中按下了按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22506298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking if a button has been pressed in python?
提问by PythonBeginner
Code:
代码:
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)
I am creating a program that simulates a vending machine. How would I tell Python to 'check' if A button has been pressed? In pseudocode it would be:
我正在创建一个模拟自动售货机的程序。我如何告诉 Python “检查”是否按下了 A 按钮?在伪代码中,它将是:
if tenbutton is pressed:
Add 10p to credit
How would I write in Python "if tenbutton is pressed"? Thank you in advance.
“如果按下十个按钮”,我将如何用 Python 编写?先感谢您。
回答by atlasologist
You can add a commandto your Tkinter Buttonwidget that will callback a function:
您可以command向Button将回调函数的Tkinter小部件添加一个:
def tenbuttonCallback():
global credit
credit += 10
tenbutton = Button(insert, text="10p", command=tenbuttonCallback)
tenbutton.grid(row=2, column=1)
回答by Dunno
It's simple, define a function which will be called after button press. Like so:
很简单,定义一个在按钮按下后将被调用的函数。像这样:
def addCredit():
global credit
credit+=10
And then assign this simple function to your button:
然后将这个简单的功能分配给您的按钮:
tenbutton = Button(insert, text="10p", command=addCredit).grid(row=2, column=1)
By the way, your code is badly asking for a classsomewhere. Using so many globals is generally a bad practice. Another nitpick is from tkinter import *, it destroys readability. I'd suggest import tkinter as tk.
顺便说一句,您的代码严重要求class某个地方。使用这么多全局变量通常是一种不好的做法。另一个挑剔的是from tkinter import *,它破坏了可读性。我建议import tkinter as tk。
回答by Gagan singh
from tkinter import *
import tkinter
import tkinter.messagebox
root = Tk()
def fun(arg):
if arg == 1:
tkinter.messagebox.showinfo("button 1", "button 1 used")
elif arg == 2:
tkinter.messagebox.showinfo("button 2", "button 2 used")
elif arg == 3:
tkinter.messagebox.showinfo("button 3", "button 3 used")
elif arg == 4:
tkinter.messagebox.showinfo("button 4", "button 4 used")
b1 = Button(root, text="Quit1", command=lambda: fun(1))
b1.pack()
b2 = Button(root, text="Quit2", command=lambda: fun(2))
b2.pack()
b3 = Button(root, text="Quit3", command=lambda: fun(3))
b3.pack()
b4 = Button(root, text="Quit4", command=lambda: fun(4))
b4.pack()
root.mainloop()

