解决python tkinter界面卡死的问题

yipeiwu_com6年前Python基础

如果点击按钮,运行了一个比较耗时的操作,那么界面会卡死。

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

方式二、继承 threading.Thread 类

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
 
songs = ['爱情买卖','朋友','回家过年','好日子'] 
films = ['阿凡达','猩球崛起']
 
 
class Application(tk.Tk):
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()     # 阻塞--会卡死界面!
    
    
app = Application()
app.mainloop()

以上这篇解决python tkinter界面卡死的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python模块文件结构代码详解

本文研究的主要是Python模块文件结构的相关内容,具体如下。 Python文件结构 文件结构(范例全文) #/usr/bin/env python "this is a...

Python编程实现数学运算求一元二次方程的实根算法示例

Python编程实现数学运算求一元二次方程的实根算法示例

本文实例讲述了Python编程实现数学运算求一元二次方程的实根算法。分享给大家供大家参考,具体如下: 问题: 请定义一个函数quadratic(a, b, c),接收3个参数,返回一元二...

Python多线程编程(八):使用Event实现线程间通信

使用threading.Event可以实现线程间相互通信,之前的Python:使用threading模块实现多线程编程七[使用Condition实现复杂同步]我们已经初步实现了线程间通信...

python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)

python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)

python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换) Python3 JSON 数据解析...

python 二分查找和快速排序实例详解

思想简单,细节颇多;本以为很简单的两个小程序,写起来发现bug频出,留此纪念。 #usr/bin/env python def binary_search(lst,t): low...