基本上,我問的是如何將不斷更新的程序顯示到tkinter的文本小部件中。Python - 關於tkinter和更新程序
from tkinter import Tk, Frame, Text, BOTH
class FrameApp(Frame):
def __init__(self, parent):
Frame.__init__(self, parent, background="white")
self.parent = parent
self.parent.title("Ethis")
self.pack(fill=BOTH, expand=1)
self.centerWindow()
def centerWindow(self):
w = 900
h = 450
sw = self.parent.winfo_screenwidth()
sh = self.parent.winfo_screenheight()
x = (sw - w)/2
y = (sh - h)/2
self.parent.geometry("%dx%d+%d+%d" % (w, h, x, y))
def theText(self):
w = Text()
def main():
root=Tk()
app = FrameApp(root)
root.mainloop()
if __name__ == '__main__':
main()
這是我的tkinter程序。正如你所看到的,我已經將它集中並設置了一個定義爲文本(自我)的文本函數。我對文本(自我)做了任何事情,因爲我不知道從哪裏開始。這個效果很好,如預期的那樣,它的中心是標題。
# Money Generator Mark 1
import time
t = 'true'
while t == 'true':
s = 0
x = 1
print ("You have $%s." % (s))
time.sleep(.75)
t = 'false'
while t == 'false':
s = s + (1 * x)
print ("You have $%s." % (s))
time.sleep(.75)
if s >= 100 and s < 200:
x = 2
if s >= 200:
x = 4
在這裏我有另一個程序,它可以正常工作。我把它叫做Money Generator,就像Cookie Clicker和Candy Box,這些類型的東西。這也適用於命令框,功能和打印到那裏。我想知道如何整合這兩個單獨的程序,以便這裏列出的第二個程序將顯示在tkinter的窗口中。 這是我的新代碼有一個新問題。我收到一個錯誤,指出'generate_money'沒有在theText函數中定義。這些新功能在我的frameApp類中。
def theText(self):
self.w = Text()
self.t = threading.Thread(target=generate_money, args=(self.w))
self.t.daemon = True
def generate_money(textwidget):
p = subprocess.Popen([sys.executable, os.path.join('window.py', 'moneygenerator.py')],
stdout = subprocess.PIPE)
for line in p.stdout:
do_stuff_with(textwidget, line)
p.close()
看起來您已經縮進了'generate_money'函數,因此它是'FrameApp'類的一部分。不要這樣做。這使'generate_money'成爲你的'FrameApp'對象的一個方法,而不是一個頂層函數。 (你可以做這個工作,但是你必須爲這個方法增加一個'self'參數,並且把它稱爲'self.generate_money'而不是'generate_money',這裏沒有很好的理由去做這件事。) – abarnert 2014-10-29 00:12:01
另外,如果你希望在後臺線程中拋出一個'NameError',除了'do_stuff_with'函數外,你還得編寫一個'do_stuff_with'函數。 – abarnert 2014-10-29 00:12:26
對不起,最後一個問題。我在課堂外移動了generate_money。不過,我仍然對do_stuff_with的內容感到困惑。我會以某種方式使用text.get()從線程獲取線條,然後使用text.insert()將它們插入到文本框中。 – TastyOs 2014-10-29 01:11:35