微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在python tkinter中的函数内部更新画布?

如何解决如何在python tkinter中的函数内部更新画布?

我是python和tkinter的新手。我最初编写了一个标准的python脚本,该脚本完全执行了我想要的操作,并在控制台运行时将信息打印到控制台。我现在正在尝试使该程序在GUI中运行。

原始程序将使用9位数字长的监视器ID(包括零),然后在一个很大的文件搜索该ID,然后读取该监视器的数据。我将程序简化了下来,以便可以在这里共享。

用户将输入监视器ID,然后单击“运行”按钮。我要程序执行的操作是更新窗口底部的状态框(我正在使用画布)。我希望它打印它正在搜索ID,然后说何时找到它。我知道为什么画布没有更新,因为GUI仅在程序返回主循环后才更新。我已经对此进行了大量研究,但无法找到使它起作用的方法。感谢您的帮助。

from tkinter import *
from tkinter import ttk

class Program:
    
    def __init__(self,master):
        master.title('My Program')
        master.resizable(False,False)
        
        self.style = ttk.Style()
        self.style.configure('TLabel',font = ('Calibri',11))
        self.style.configure('Header.TLabel',18,'bold'))
        
        # Create header frame for header and description.
        self.topframe = ttk.Frame(master)
        self.topframe.pack()
        
        # Put the header label in the top frame.
        ttk.Label(self.topframe,text = 'My Program',style = 'Header.TLabel').grid(row = 0,column = 0)
        
        # Put the description label below the header label.
        ttk.Label(self.topframe,wraplength = 400,text = 'This is a test program.').grid(row = 1,column = 0)
    
        # Create the frame for the entry and button.
        self.botframe = ttk.Frame(master)
        self.botframe.pack()
    
        # Put in the label and entry form.
        ttk.Label(self.botframe,text = 'Monitor ID (9 digits,no dashes):').grid(row = 0,column = 0,padx = 5,sticky = 'w')
        self.monitor = ttk.Entry(self.botframe,width = 9,font = ('Arial',10))
        self.monitor.grid(row = 0,column = 1,pady = 5,sticky = 'w')
        
        # Put in the button to run the program.
        ttk.Button(self.botframe,text = "RUN",command = self.run).grid(row = 1,sticky = 'w')
        
        # Create the status frame.
        self.statframe = ttk.LabelFrame(master,text = 'Status')
        self.statframe.pack()
        
        self.canvas = Canvas(self.statframe)
        self.canvas.pack()
        self.canvas.config(width = 400,height = 50)
        self.status = self.canvas.create_text(200,25,text = 'Status Update')
            
    def run(self):
        self.canvas.delete('all')
        self.AQS_id = self.monitor.get()
        
        if len(self.AQS_id) != 9:
            messageBox.showerror(title = "Error",message = 'The site ID should be 9 characters. Try again.')
            return
        
        self.canvas.delete('all')
        self.status = self.canvas.create_text(200,text = f'Searching for monitor: {self.AQS_id}')
        
        # Loop through counter until it matches the mon_id.
        mon_id = int(self.AQS_id)
        i = 0
        
        while i <= 999999999:
            if i == mon_id:
                self.canvas.delete('all')
                self.status = self.canvas.create_text(200,text = 'Found monitor')
                break
            else:
                i += 1

root = Tk() 
program = Program(root)
root.mainloop()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。