tkinter介面卡死的解決辦法

2022-08-19 17:36:13 字數 3658 閱讀 4277

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.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 = ['阿凡達','猩球崛起']

def music(songs):

global text # 故意的,注意與movie的區別

for s in songs:

text.insert(tk.end, "聽歌曲:%s \t-- %s\n" %(s, time.ctime()))

time.sleep(3)

def movie(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 = ['阿凡達','猩球崛起']

def music(songs):

global text # 故意的,注意與movie的區別

for s in songs:

text.insert(tk.end, "聽歌曲:%s \t-- %s\n" %(s, time.ctime()))

time.sleep(3)

def movie(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() # 在這裡開始

def run(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

def __init__(self):

super().__init__()

self.createui()

# 生成介面

def createui(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)

# 邏輯:聽**

def music(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)

# 邏輯:看電影

def movie(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 defonclick text,i time.sleep 3 text.insert tk.end,按了第 個按鈕 n format i root tk.tk text tk.text root text.pack tk.button...

解決介面卡死的問題

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...