简体   繁体   English

如何更新tkinter中的“live”文本框?

[英]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: 还有一点:

  • 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 mainloop and your tkinter import is inconsistent with the way you use the classes (without tk prefix) 可能只是一个复制粘贴错误,但你忘了打电话给mainloop和您的tkinter导入与正在使用的类(没有办法不一致的tk前缀)
  • 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 您可以在创建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.

相关问题 如何在Tkinter中更新此文本框的文本? - How do I update this Text Box's text in Tkinter? 如何根据框中是否有文本禁用 Python tkinter 中的按钮? - How can I disable a button in Python tkinter based on if there is text in the box or not? 如何在 tkinter 文本框中使用带有 html 标签的文本,或更改它以使其在 tkinter ZD304BA20E96D873411 中工作? - How can I use text with html tags in a tkinter text box, or change it so that it works in a tkinter label? 在 Tkinter 中的每个条目之后,如何更新 label 上的文本? - How can I update the text on a label after each entry in Tkinter? 如何在 Python tkinter 中的文本框中插入文本和变量而不包含 {} - How can I insert text and variables into a text box in Python tkinter without elements being contained by {} 有没有办法用文字更新 Tkinter canvas ? - Is there a way to update Tkinter canvas live with text? 在 tkinter 如何将文本框中的文本作为变量访问 - in tkinter how do I access text in a text box as a variable 如何将输入输入框 (tkinter) 的文本分配给 python 脚本中的变量并通过按下按钮运行脚本? - How can I assign the text imputed into an entry box (tkinter) to variable in python script and run the script by pressing a button? 我如何让Tkinter程序实时扫描文本框以获取输入? - How can I have a Tkinter program scan a text box for inputs in real time? 如何将滚动条附加到 tkinter 上的文本框? - How can i attach my scroll bar to my text box on tkinter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM