一個線程可以,兩個卡死
#!/usr/bin/python
# coding: utf-8
from Tkinter import *
import threading
class MyThread2(threading.Thread):
def __init__(self, func):
threading.Thread.__init__(self)
self.func = func
def run(self):
self.func()
def windows1():
root = Tk()
root.title("記事本")
# root.geometry("800x600+100+100")
s1 = Scrollbar(root)
s1.pack(side=RIGHT, fill=Y)
# HORIZONTAL 設置水平方向的滾動條,默認是豎直
s2 = Scrollbar(root, orient=HORIZONTAL)
s2.pack(side=BOTTOM, fill=X)
# 創建文本框
# wrap 設置不自動換行
textpad = Text(root, yscrollcommand=s1.set, xscrollcommand=s2.set, wrap='none')
textpad.pack(expand=YES, fill=BOTH)
s1.config(command=textpad.yview)
s2.config(command=textpad.xview)
root.mainloop()
def windows2():
root = Tk()
root.title("記事本")
# root.geometry("800x600+100+100")
s1 = Scrollbar(root)
s1.pack(side=RIGHT, fill=Y)
# HORIZONTAL 設置水平方向的滾動條,默認是豎直
s2 = Scrollbar(root, orient=HORIZONTAL)
s2.pack(side=BOTTOM, fill=X)
# 創建文本框
# wrap 設置不自動換行
textpad = Text(root, yscrollcommand=s1.set, xscrollcommand=s2.set, wrap='none')
textpad.pack(expand=YES, fill=BOTH)
s1.config(command=textpad.yview)
s2.config(command=textpad.xview)
root.mainloop()
if __name__ == '__main__':
threads = []
t1 = MyThread2(windows1)
threads.append(t1)
t2 = MyThread2(windows2)
threads.append(t2)
for t in threads:
t.setDaemon(True)
t.start()
for t in threads:
t.join()
運行一直等待中,不會出窗口
網上搜的結果,待驗證。
tkinter和多線程確實有沖突,我也是弄了很久,最后才發現是這兩者的問題。
后來我又自學了c#,發現其圖形界面的控件是不允許多線程直接訪問的,所以我推測python是因為圖形下的控件被多線程同時訪問(賦值),所以造成崩潰。
解決辦法,我目前知道的(我是新手),要么不用多線程,要么不用tkinter。
