Tkinter 与主循环一起管理我的事件循环

for*_*ord 2 python user-interface loops tkinter widget

我一直在慢慢学习 Tkinter 和面向对象编程,但我已经把自己编程到了这个角落。请原谅我对这个问题缺乏批判性思维,但我已经问过我认识的每个比我更了解 Python 的人,我们无法在这里找到可行的解决方案。

我有一个正在开发的 gui 应用程序,旨在允许用户输入股票代码,为每个代码创建新标签,然后定期更新每个标签。(有点像一个非常基本的 etrade 应用程序或其他东西)。我发现在没有 gui 的情况下很容易做到这一点,因为我可以说:

while True:
   sPrice = get_stock_price(s)
   print sPrice
Run Code Online (Sandbox Code Playgroud)

但我已将我的 get_stock_price(s) 函数绑定到一个按钮,该按钮会生成一个子框架和一个包含在其中的标签。我遇到的问题是标签不会更新。一个朋友建议添加另一种方法来更新标签,但是我知道如何持续更新它的唯一方法是做一个

while True:
    # get new price
    # update the label
    # time.sleep(~1 minute?)
Run Code Online (Sandbox Code Playgroud)

这会导致我的 gui 窗口永远冻结和旋转。我一直在阅读与此特定情况相关的所有其他线程,并且看到了许多不同的建议;不要在主线程中调用 sleep,不要使用 root.update,使用事件,调用 root.something.after(500, function) 并且我已经尝试实现。我留下的是一个科学怪人的代码,它仍然会检索我的股票价值,但不会更新它们,以及一些我不知道如何更新或在我的代码中调用的方法。

我希望的是(可能很长,我知道。对不起!)对我做错了什么的解释,以及如何解决它的建议。我真的很想自己理解和解决这个问题,但是只要解释了代码解决方案就会很棒。

非常感谢提前!!!

PS:这是我到目前为止的代码:

from Tkinter import *
import urllib
import re
import time

class MyApp(object):
    def __init__(self, parent):
        self.myParent = parent
        self.myContainer1 = Frame(parent)
        self.myContainer1.pack()
        self.createWidgets()
        button1 = Button(self.myContainer1, command = self.addStockToTrack)
        self.myContainer1.bind("<Return>", self.addStockToTrack)
        button1.configure(text = "Add Symbol")
        button1.pack()  

    def createWidgets(self):
        # title name
        root.title("Stock App")
        # creates a frame inside myContainer1
        self.widgetFrame = Frame(self.myContainer1)
        self.widgetFrame.pack()
        # User enters stock symbol here:
        self.symbol = Entry(self.widgetFrame) 
        self.symbol.pack()
        self.symbol.focus_set()

    def addStockToTrack(self):
        s = self.symbol.get()
        labelName = str(s) + "Label"
        self.symbol.delete(0, END)
        stockPrice = get_quote(s)
        self.labelName = Label(self.myContainer1, text = s.upper() + ": " + str(stockPrice))
        self.labelName.pack()
        self.myContainer1.after(500, self.get_quote)

    def updateStock(self):
        while True:
            labelName = str(s) + "Label"
            stockPrice = get_quote(s)
            self.labelName = Label(self.myContainer1, text = s.upper() + ": " + str(stockPrice))
            self.labelName.pack()
            time.sleep(10)

def get_quote(symbol):
    base_url = 'http://finance.google.com/finance?q='
    content = urllib.urlopen(base_url + symbol).read()
    m = re.search('id="ref_\d*_l".*?>(.*?)<', content)
    if m:
        quote = m.group(1)
    else:
        quote = 'Not found: ' + symbol
    return quote

root = Tk()
myapp = MyApp(root)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)

Bry*_*ley 5

您已经有一个无限循环在运行,因此您不应该尝试添加另一个无限循环。相反,您可以使用该after方法使函数每隔一段时间重复调用一次。在你的情况下,你可以替换这个:

def updateStock(self):
   while True:
        labelName = str(s) + "Label"
        stockPrice = get_quote(s)
        self.labelName = Label(self.myContainer1, text = s.upper() + ": " + str(stockPrice))
        self.labelName.pack()
        time.sleep(10)
Run Code Online (Sandbox Code Playgroud)

... 有了这个:

def updateStock(self):
    labelName = str(s) + "Label"
    stockPrice = get_quote()
    self.labelName = Label(self.myContainer1, text = s.upper() + ": " + str(stockPrice))
    self.labelName.pack()
    self.after(10000, self.updateStock)
Run Code Online (Sandbox Code Playgroud)

这将获得一个报价,添加一个标签,然后安排自己在 10 秒(10,000 毫秒)内再次被调用。

但是,我怀疑您是否想每 10 秒创建一个新标签,是吗?最终窗口将填满标签。相反,您可以创建一次标签,然后在每次迭代中更新标签。例如,self.label在 init 中创建一次,然后在循环中您可以执行以下操作:

self.labelName.configure(text=s.upper() + ": " + str(stockPrice))
Run Code Online (Sandbox Code Playgroud)