欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

解決python tkinter界面卡死的問(wèn)題

 更新時(shí)間:2019年07月17日 09:57:49   作者:羅兵の水庫(kù)  
今天小編就為大家分享一篇解決python tkinter界面卡死的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

如果點(diǎn)擊按鈕,運(yùn)行了一個(gè)比較耗時(shí)的操作,那么界面會(huì)卡死。

import tkinter as tk
import time
 
def onclick(text, i):
  time.sleep(3)
  text.insert(tk.END, '按了第{}個(gè)按鈕\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()

解決辦法:

方式一、直接開(kāi)線程

import tkinter as tk
import time
import threading
 
 
songs = ['愛(ài)情買(mǎi)賣','朋友','回家過(guò)年','好日子']
movies = ['阿凡達(dá)','猩球崛起']
 
def music(songs):
  global text # 故意的,注意與movie的區(qū)別
  for s in songs:
    text.insert(tk.END, "聽(tīng)歌曲:%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):
  '''將函數(shù)打包進(jìn)線程'''
  # 創(chuàng)建
  t = threading.Thread(target=func, args=args) 
  # 守護(hù) !!!
  t.setDaemon(True) 
  # 啟動(dòng)
  t.start()
  # 阻塞--卡死界面!
  # t.join()
 
 
root = tk.Tk()
 
text = tk.Text(root)
text.pack()
 
tk.Button(root, text='音樂(lè)', 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 = ['愛(ài)情買(mǎi)賣','朋友','回家過(guò)年','好日子']
movies = ['阿凡達(dá)','猩球崛起']
 
def music(songs):
  global text # 故意的,注意與movie的區(qū)別
  for s in songs:
    text.insert(tk.END, "聽(tīng)歌曲:%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()  # 在這里開(kāi)始
    
  def run(self):
    self.func(*self.args)
  
 
root = tk.Tk()
 
text = tk.Text(root)
text.pack()
 
tk.Button(root, text='音樂(lè)', command=lambda :MyThread(music, songs)).pack()
tk.Button(root, text='電影', command=lambda :MyThread(movie, movies, text)).pack()
 
root.mainloop()

三、或者,搞一個(gè)界面類:

import tkinter as tk
import time
import threading
 
songs = ['愛(ài)情買(mǎi)賣','朋友','回家過(guò)年','好日子'] 
films = ['阿凡達(dá)','猩球崛起']
 
 
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='音樂(lè)', command=lambda :self.thread_it(self.music, songs)).pack(expand=True, side=tk.RIGHT) # 注意lambda語(yǔ)句的作用!
    tk.Button(self, text='電影', command=lambda :self.thread_it(self.movie, films)).pack(expand=True, side=tk.LEFT)
    
 
  # 邏輯:聽(tīng)音樂(lè)
  def music(self, songs):
    for x in songs:
      self.text.insert(tk.END, "聽(tīng)歌曲:%s \t-- %s\n" %(x, time.ctime()))
      print("聽(tīng)歌曲:%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)
 
  # 打包進(jìn)線程(耗時(shí)的操作)
  @staticmethod
  def thread_it(func, *args):
    t = threading.Thread(target=func, args=args) 
    t.setDaemon(True)  # 守護(hù)--就算主界面關(guān)閉,線程也會(huì)留守后臺(tái)運(yùn)行(不對(duì)!)
    t.start()      # 啟動(dòng)
    # t.join()     # 阻塞--會(huì)卡死界面!
    
    
app = Application()
app.mainloop()

以上這篇解決python tkinter界面卡死的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論