tkinter介面卡死的解決辦法

2021-09-07 05:40:50 字數 4277 閱讀 4015

import

tkinter as tk

import

time

defonclick(text, i):

time.sleep(3)

text.insert(tk.end,

'按了第{}個按鈕\n

'.format(i))

root =tk.tk()

text =tk.text(root)

text.pack()

tk.button(root, text='

按鈕1', command=lambda :onclick(text,1)).pack()

tk.button(root, text='

按鈕2', command=lambda :onclick(text,2)).pack()

解決辦法:

import

tkinter as tk

import

time

import

threading

songs = ['

愛情買賣

','朋友

','回家過年

','好日子']

movies = ['

阿凡達','

猩球崛起']

defmusic(songs):

global text #

故意的,注意與movie的區別

for s in

songs:

text.insert(tk.end,

"聽歌曲:%s \t-- %s\n

" %(s, time.ctime()))

time.sleep(3)

defmovie(movies, text):

for m in

movies:

text.insert(tk.end,

"看電影:%s \t-- %s\n

" %(m, time.ctime()))

time.sleep(5)

def thread_it(func, *args):

'''將函式打包進執行緒

'''#

建立 t = threading.thread(target=func, args=args)

#守護 !!!

t.setdaemon(true)

#啟動t.start()

#阻塞--卡死介面!

#t.join()

root =tk.tk()

text =tk.text(root)

text.pack()

tk.button(root, text='

**', command=lambda

:thread_it(music, songs)).pack()

tk.button(root, text='

電影', command=lambda

:thread_it(movie, movies, text)).pack()

root.mainloop()

import

tkinter as tk

import

time

import

threading

songs = ['

愛情買賣

','朋友

','回家過年

','好日子']

movies = ['

阿凡達','

猩球崛起']

defmusic(songs):

global text #

故意的,注意與movie的區別

for s in

songs:

text.insert(tk.end,

"聽歌曲:%s \t-- %s\n

" %(s, time.ctime()))

time.sleep(3)

defmovie(movies, text):

for m in

movies:

text.insert(tk.end,

"看電影:%s \t-- %s\n

" %(m, time.ctime()))

time.sleep(5)

class

mythread(threading.thread):

def__init__(self, func, *args):

super().

__init__

()

self.func =func

self.args =args

self.setdaemon(true)

self.start()

#在這裡開始

defrun(self):

self.func(*self.args)

root =tk.tk()

text =tk.text(root)

text.pack()

tk.button(root, text='

**', command=lambda

:mythread(music, songs)).pack()

tk.button(root, text='

電影', command=lambda

:mythread(movie, movies, text)).pack()

root.mainloop()

import

tkinter as tk

import

time

import

threading

songs = ['愛情買賣','朋友','回家過年','好日子']

films = ['阿凡達','猩球崛起']

class

def__init__

(self):

super().

__init__

()

self.createui()

#生成介面

defcreateui(self):

self.text =tk.text(self)

self.text.pack()

tk.button(self, text='

**', command=lambda :self.thread_it(self.music, songs)).pack(expand=true, side=tk.right) #

注意lambda語句的作用!

tk.button(self, text='

電影', command=lambda :self.thread_it(self.movie, films)).pack(expand=true, side=tk.left)

#邏輯:聽**

defmusic(self, songs):

for x in

songs:

self.text.insert(tk.end,

"聽歌曲:%s \t-- %s\n

" %(x, time.ctime()))

print("

聽歌曲:%s \t-- %s

" %(x, time.ctime()))

time.sleep(3)

#邏輯:看電影

defmovie(self, films):

for x in

films:

self.text.insert(tk.end,

"看電影:%s \t-- %s\n

" %(x, time.ctime()))

print("

看電影:%s \t-- %s

" %(x, time.ctime()))

time.sleep(5)

#打包進執行緒(耗時的操作)

@staticmethod

def thread_it(func, *args):

t = threading.thread(target=func, args=args)

t.setdaemon(true)

#守護--就算主介面關閉,執行緒也會留守後台執行(不對!)

t.start() #啟動#

t.join() # 阻塞--會卡死介面!

tkinter介面卡死的解決辦法

import tkinter as tk import time def onclick text,i time.sleep 3 text.insert tk.end,按了第 個按鈕 n format i root tk.tk text tk.text root text.pack tk.butto...

解決介面卡死的問題

private void textbox2 textchanged object sender,eventargs e objthread.start 新增以下 忽略控制項多執行緒的安全機制 可實現,但不建議直接建立執行緒操作控制項,控制項上的大多數方法只能從建立控制項的執行緒呼叫 control....

MFC 介面卡死到底為何

現象 程式執行一段時間後,介面卡死 程式開啟了乙個監聽執行緒 建立接收訊號線程 thread afxbeginthread getiomsgthread,this,thread priority normal,0,0,null int cfeecard multidlg maingetiomsg l...