[英]How can I update a text box 'live' in tkinter?
I would like to ask how would I go about maybe creating a 'LIVE' text box in python? 我想问一下如何在python中创建一个“LIVE”文本框? This program is a simulator for a vending machine (code below). 该程序是自动售货机的模拟器(下面的代码)。 I want there to be a text box showing a live credit update How do you do that in tkinter? 我希望有一个显示实时信用更新的文本框你如何在tkinter中做到这一点?
For Example: Say there is a box for credit with 0 inside it in the middle of the window. 例如:假设在窗口中间有一个信用卡,其中包含0。 When the 10p
button is pressed the box for credit should change from '0' to '0.10'. 按下10p
按钮时,信用卡框应从“0”变为“0.10”。 Is it possible to do thit in tkinter and python 3.3.2? 有没有可能在tkinter和python 3.3.2中做? Thank you in advance! 先感谢您!
import sys
import tkinter as tk
credit = 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 addTENp():
global credit
credit+=0.10
def addTWENTYp():
global credit
credit+=0.20
def addFIFTYp():
global credit
credit+=0.50
def addPOUND():
global credit
credit+=1.00
def insert():
insert = Tk()
insert.geometry("480x360")
iLabel = Label(insert, text="Enter coins.[Press Buttons]").grid(row=1, column=1)
tenbutton = Button(insert, text="10p", command = addTENp).grid(row=2, column=1)
twentybutton = Button(insert, text="20p", command = addTWENTYp).grid(row=3, column=1)
fiftybutton = Button(insert, text="50p", command = addFIFTYp).grid(row=4, column=1)
poundbutton = Button(insert, text="£1", command = addPOUND).grid(row=5, column=1)
insert()
Sure you can! 你当然可以! Just add another label to the frame, and update the text
attribute whenever one of your add functions is called. 只需在框架中添加另一个标签,并在调用其中一个添加函数时更新text
属性。 Also, you can simplify that code, using one add
function for all the different amounts. 此外,您可以使用一个add
函数来简化该代码,以用于所有不同的金额。
def main():
frame = Tk()
frame.geometry("480x360")
Label(frame, text="Enter coins.[Press Buttons]").grid(row=1, column=1)
display = Label(frame, text="") # we need this Label as a variable!
display.grid(row=2, column=1)
def add(amount):
global credit
credit += amount
display.configure(text="%.2f" % credit)
Button(frame, text="10p", command=lambda: add(.1)).grid(row=3, column=1)
Button(frame, text="20p", command=lambda: add(.2)).grid(row=4, column=1)
Button(frame, text="50p", command=lambda: add(.5)).grid(row=5, column=1)
Button(frame, text="P1", command=lambda: add(1.)).grid(row=6, column=1)
frame.mainloop()
main()
Some more points: 还有一点:
mainloop
and your tkinter
import is inconsistent with the way you use the classes (without tk
prefix) 可能只是一个复制粘贴错误,但你忘了打电话给mainloop
和您的tkinter
导入与正在使用的类(没有办法不一致的tk
前缀) None
您可以在创建GUI元素后立即进行布局,但请注意,在这种情况下,GUI元素不会绑定到变量,而是布局函数的结果,即None
Borrowing a framework from tobias_k's excellent answer, I would recommend you use a DoubleVar
instead. 从tobias_k的优秀答案中借用一个框架,我建议你改用DoubleVar
。
from tkinter import ttk
import tkinter as tk
def main():
frame = Tk()
frame.geometry("480x360")
credit = tk.DoubleVar(frame, value=0)
# credit = tk.StringVar(frame, value="0")
ttk.Label(frame, textvariable = credit).pack()
def add_credit(amt):
global credit
credit.set(credit.get() + amt)
# new_credit = str(int(credit.get().replace(".",""))+amt)
# credit.set(new_credit[:-2]+"."+new_credit[-2:])
ttk.Button(frame, text="10p", command = lambda: add_credit(0.1)).pack()
# ttk.Button(frame, text="10p", command = lambda: add_credit(10)).pack()
ttk.Button(frame, text="20p", command = lambda: add_credit(0.2)).pack()
# ttk.Button(frame, text="20p", command = lambda: add_credit(20)).pack()
ttk.Button(frame, text="50p", command = lambda: add_credit(0.5)).pack()
# ttk.Button(frame, text="50p", command = lambda: add_credit(50)).pack()
ttk.Button(frame, text="P1", command = lambda: add_credit(1.0)).pack()
# ttk.Button(frame, text="P1", command = lambda: add_credit(100)).pack()
frame.mainloop()
The comments in that code is an alternate implementation that will work better, if only just. 该代码中的注释是一个替代实现,如果只是公正的话,它将更好地工作。 This will guarantee you won't have any strange floating-point errors in your code. 这将保证您的代码中不会出现任何奇怪的浮点错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.