简体   繁体   English

Python / Tkinter动态更新标签

[英]Python/Tkinter dynamically update label

I am currently trying to make a GUI to an existing python program using Tkinter. 我目前正在尝试使用Tkinter为现有的python程序创建一个GUI。 The program gives the user two options from which the user must choose to either accept or decline. 该程序为用户提供了两个选项,用户必须从中选择接受或拒绝。 Before using Tkinter the options were placed in the terminal and awaited for a raw_input. 在使用Tkinter之前,选项被放置在终端中并等待raw_input。 (y/n). (Y / N)。 How can I make this so the canvas text updates with the new data and awaits for the users button click? 我怎样才能使画布文本更新为新数据并等待用户按钮点击?

To make my question more specific: How can I run another programs code while the Tkinter mainloop is running and make these two interact? 为了使我的问题更具体:如何在Tkinter主循环运行时运行另一个程序代码并使这两个代码交互?

Example code below. 示例代码如下。

from Tkinter import *

root = Tk()
root.resizable(width=False, height=False)
root.geometry('{}x{}'.format(500,550))
root.wm_title("Tkinter test")

BtnFrame = Frame (root)
BtnFrame.pack(side = BOTTOM)
BtnFrame.place(y=450, x=20)

canvas_1 = Canvas(root, width = "200", height ="300")
canvas_2 = Canvas(root, width = "250", height ="300")
canvas_1.pack(side = LEFT)
canvas_2.pack(side = RIGHT)

textfield_1 = canvas_1.create_text(100,50)
textfield_2 = canvas_2.create_text(100,50,)


def update_textfiel_1(text):
   global textfield_1
   canvas_1.delete(textfield_1)
   textfield = canvas.create_text(100,50,text = text)

def update_textfiel_2(text):
   global textfield_2
   canvas_2.delete(textfield_2)
   textfield1 = canvas1.create_text(100,50,text = text)



 Accept = Button(BtnFrame, text="Accept", width=25)
 Decline = Button(BtnFrame, text="Decline", width=25)

 Accept.pack(side = LEFT)
 Decline.pack(side = RIGHT)

 root.mainloop()

First off you have some inconsistent variable names in your update_textfiel functions, you can greatly simplify it by using .itemconfigure ( documentation for methods on canvas widget ) 首先,你的update_textfiel函数中有一些不一致的变量名,你可以通过使用.itemconfigurecanvas小部件上的方法的文档)来大大简化它。

def update_textfiel_1(new_text):
   canvas_1.itemconfigure(textfield_1, text=new_text)

def update_textfiel_2(new_text):
   canvas_2.itemconfigure(textfield_2, text=new_text)

If I understand correctly you want a way to have a function that will simply wait for the user to press one of the buttons and then return the result, this is very easy with tkMessageBox : 如果我理解正确你想要一种方法来拥有一个只需要等待用户按下其中一个按钮然后返回结果的功能,这对于tkMessageBox来说非常简单:

question = """Do you accept {}?
if you say no you will instead get {}"""
#this message can GREATLY be improved
# But I really don't understand what you are using it for...

def user_confirmation(name1, name2):
    response = tkMessageBox.askyesno("Accept or Decline",question.format(name1,name2))
    print(response)
    if response: # == True
        return name1
    else:
        return name2

I have not yet found a way to make a blocking function that works with the window you have currently... 我还没有找到一种方法来制作一个与当前窗口一起使用的阻塞功能...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM