Python 如何在 tkinter 中更新文本框“实时”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22519047/
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
How can I update a text box 'live' in tkinter?
提问by PythonBeginner
I would like to ask how would I go about maybe creating a 'LIVE' text box in python? 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?
我想问一下我将如何在 python 中创建一个“LIVE”文本框?该程序是自动售货机的模拟器(代码如下)。我希望有一个文本框显示实时信用更新你如何在 tkinter 中做到这一点?
For Example: Say there is a box for credit with 0 inside it in the middle of the window. When the 10pbutton is pressed the box for credit should change from '0' to '0.10'.
Is it possible to do thit in tkinter and python 3.3.2?
Thank you in advance!
例如:假设窗口中间有一个信用框,里面有 0。当10p按下按钮时,信用框应从“0”变为“0.10”。是否可以在 tkinter 和 python 3.3.2 中做到这一点?先感谢您!
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()
采纳答案by tobias_k
Sure you can! Just add another label to the frame, and update the textattribute whenever one of your add functions is called. Also, you can simplify that code, using one addfunction for all the different amounts.
你当然可以!只需向框架添加另一个标签,并在text调用添加函数之一时更新属性。此外,您可以简化该代码,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:
还有几点:
- note that you define many of your variables twice
- you should not give a variable the same name as a function, as this will shadow the function
- probably just a copy paste error, but you forgot to call
mainloopand yourtkinterimport is inconsistent with the way you use the classes (withouttkprefix) - you can do the layout right after creating the GUI elements, but note that in this case not the GUI element will be bound to the variable, but the result of the layouting function, which is
None
- 请注意,您两次定义了许多变量
- 你不应该给一个与函数同名的变量,因为这会影响函数
- 可能只是复制粘贴错误,但您忘记调用
mainloop并且您的tkinter导入与您使用类的方式不一致(没有tk前缀) - 您可以在创建 GUI 元素后立即进行布局,但请注意,在这种情况下,不是 GUI 元素将绑定到变量,而是布局函数的结果,即
None
回答by Adam Smith
Borrowing a framework from tobias_k's excellent answer, I would recommend you use a DoubleVarinstead.
从 tobias_k 的优秀答案中借用一个框架,我建议您改用 a 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.
该代码中的注释是一种替代实现,即使只是这样,它也会更好地工作。这将保证您的代码中不会出现任何奇怪的浮点错误。

