在Python中使用while循环进行窗口更新

-2 投票
1 回答
1546 浏览
提问于 2025-04-18 14:36

我写了一个脚本,目的是把一个变量放到插入字段里。到目前为止,我已经能创建一个while循环来打印这个变量,所以我知道它是有效的。

让我感到沮丧的是,它并没有弹出窗口,只是打印了这个变量:

import time
import subprocess
from Tkinter import *
import urllib2
from ttk import *
import Tkinter as tk

root = Tk()

a= 0
while a<1:

    print a


    class Application(Frame):
            """GUI to display results of 'equity get'"""

            def __init__(self, master):
                """initialise the Frame"""
                Frame.__init__(self,master)
                self.grid()
                self.create_widgets()

            def create_widgets(self):
                """Create button, text and entry Widget"""
                """what it is i.e. label"""


                self.data = Label(self, text ="Data")
                self.data1 = Entry(self, width = 10)


                self.data.grid(column = 1, row = 1)
                self.data1.grid(column = 2, row = 1)
                self.data1.grid(column = 3, row = 1)            
                self.data1.insert(0,a)

app = Application(root)
root.title("reload test")
root.geometry("700x300")
root.mainloop()

下面的版本可以打开窗口,但循环只执行了一次,而实际上,我希望它能一直运行。

import time
import subprocess
from Tkinter import *
import urllib2
from ttk import *
import Tkinter as tk

root = Tk()

a= 0
while a<1:

    print a


    class Application(Frame):
            """GUI to display results of 'equity get'"""

            def __init__(self, master):
                """initialise the Frame"""
                Frame.__init__(self,master)
                self.grid()
                self.create_widgets()

            def create_widgets(self):
                """Create button, text and entry Widget"""
                """what it is i.e. label"""


                self.data = Label(self, text ="Data")
                self.data1 = Entry(self, width = 10)


                self.data.grid(column = 1, row = 1)
                self.data1.grid(column = 2, row = 1)
                self.data1.grid(column = 3, row = 1)            
                self.data1.insert(0,a)

    app = Application(root)
    root.title("reload test")
    root.geometry("700x200")
    root.mainloop()

谢谢@guest提到的after函数,不过我有点不确定它应该放在哪里。

@Wooble,变量a将是一个网页抓取的结果,我想把实时信息插入到我的插入字段中。因此,它会运行一个循环,把找到的内容插入进去!

也许我把after函数放错地方了!

import time
import subprocess
from Tkinter import *
import urllib2
from ttk import *
import Tkinter as tk

root = Tk()

a= 0
while a<1:
    url = "https://uk.finance.yahoo.com/q?s=ngq14.nym&ql=1"
    request= urllib2.Request(url)
    handle = urllib2.urlopen(request)
    content = handle.read()
    splitted_page24 = content.split("<span id=\"yfs_l10_ngq14.nym\">", 1);
    splitted_page24 = splitted_page24[1].split("</span>", 1)
    print splitted_page24[0]


    class Application(Frame):
            """GUI to display results of 'equity get'"""

            def __init__(self, master):
                """initialise the Frame"""
                Frame.__init__(self,master)
                self.grid()
                self.create_widgets()

            def create_widgets(self):
                """Create button, text and entry Widget"""
                """what it is i.e. label"""


                self.data = Label(self, text ="Data")
                self.data1 = Entry(self, width = 10)


                self.data.grid(column = 1, row = 1)
                self.data1.grid(column = 2, row = 1)
                self.data1.grid(column = 3, row = 1)            
                self.data1.insert(0,splitted_page24[0])

    app = Application(root)
    root.title("reload test")
    root.geometry("700x300")
    root.mainloop()
    root.after(15)                    

1 个回答

0

你代码里最大的问题可能就是在一个循环里调用了 mainloop。这样做完全没有意义。Tkinter 这个库是设计成你只需要调用一次 mainloop,通常是在你程序的最后一行,或者在你主函数的最后一行。

在循环里定义一个类也没有道理。我觉得你的代码需要彻底重写。

最后,你看不到窗口内容的原因是因为你没有请求让它们可见。你创建了一个 Application 的实例,它是 Frame 的一个子类。但是,你从来没有在这个实例上调用 packplacegrid,所以它一直是不可见的。

撰写回答