-
Python tkinter 多线程 哲学家进餐问题
2020-12-21 07:39:03Python tkinter 多线程 哲学家进餐问题 网上找了好久都没有自己满意的代码,于是自己学了一个,不过这只是个雏形,要想变成你满意的程序,需要靓男靓女们自己的努力啦! 废话不多说,直接上代码! from tkinter ... -
tkinter多线程刷新界面
2021-02-19 16:48:26环境:win10 + Python3.9 + tkinter8.6.9 import tkinter as tk from tkinter import ttk import threading, time, sys, queue def fmtTime(timeStamp): timeArray = time.localtime(timeStamp) dat参考:https://blog.csdn.net/u013700771/article/details/103321783
环境:win10 + Python3.9 + tkinter8.6.9
import tkinter as tk from tkinter import ttk import threading, time, sys, queue def fmtTime(timeStamp): timeArray = time.localtime(timeStamp) dateTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray) return dateTime class GUI(): def __init__(self, root): #new 一个Quue用于保存输出内容 self.msg_queue = queue.Queue() self.initGUI(root) #在show_msg方法里,从Queue取出元素,输出到Text def show_msg(self): while not self.msg_queue.empty(): content = self.msg_queue.get() self.text.insert("insert", content) self.text.see("end") #after方法再次调用show_msg self.root.after(100, self.show_msg) def initGUI(self, root): self.root = root self.root.title("test") self.root.geometry("400x200+700+500") self.root.resizable = False self.btn = ttk.Button(self.root, text="click", takefocus=0, command=self.show) self.btn.pack(side="top") self.scrollBar = ttk.Scrollbar(self.root) self.scrollBar.pack(side="right", fill="y") self.text = tk.Text(self.root, height=10, bd=1, relief="solid", yscrollcommand=self.scrollBar.set) self.text.pack(side="top", fill="both", padx=10, pady=10) self.scrollBar.config(command=self.text.yview) #启动after方法 self.root.after(100, self.show_msg) root.mainloop() def __show(self): i = 0 while i < 3: # print(fmtTime(time.time())) self.msg_queue.put(fmtTime(time.time())+"\n") time.sleep(1) i += 1 def show(self): T = threading.Thread(target=self.__show, args=()) T.start() if __name__ == "__main__": root = tk.Tk() myGUI = GUI(root)
如下图:
-
pythontk多线程_Python Tkinter多线程功能
2020-12-10 08:27:36I am currently working on an Email sender and recieving program with the tkinter library from python.I am using the threading module to make the program refresh the unread emails every 60 seconds whil...I am currently working on an Email sender and recieving program with the tkinter library from python.
I am using the threading module to make the program refresh the unread emails every 60 seconds while you can still continue doing stuff in the program.
The threading module works when just making a print("something") command, and I can still continue doing stuff in the program. However, when I make the thread log into gmail and getting the unread email count, the whole program freezes and crashes.
Below is a snippet of my code. I will not post the full code, I made a short version to show how it looks.
EDIT:
Made a small fault in the function. the get_credentials() is removed.
import tkinter, re, threading, time, imaplib, too many to list here.
class Application(Frame):
def __init__(self, parent):
... Start some functions
... Create some widgets
... Create some global stringvars for entry fields
def threadrefresh(self):#I want to start this function when a button is clicked
def multithreading():
usernamevar = "Username"
passwordvar = "Password"
obj = imaplib.IMAP4_SSL('imap.gmail.com', '993') #connect to gmail
obj.login(usernamevar, passwordvar) #log in
obj.select() #select the inbox
unread = str(len(obj.search(None, 'UnSeen')[1][0].split())) #get the total unread
print(unread)
obj.close()
time.sleep(3)
multi = threading.Thread(target=multithreading)
multi.start()
multi = threading.Thread(target=multithreading)
multi.start()
def other_functions_that_do_not_matter_in_this_case():
... Creating GUI
... Sending mail
... Etc.
... Create a button with function call self.threadrefresh
def main():
root = Tk()
app = Application(root)
root.mainloop()
if __name__ == '__main__':
main()
解决方案
Is this code actually correct?
You call this in multithreading:
time.sleep(3)
multi = threading.Thread(target=multithreading)
multi.start()
You are basically telling every thread to create a copy of itself after 3 seconds... I think you are missing the point of a thread. You should probably have a (single) thread running in a while loop that gets data from a Queue.
Whenever you want the thread to act on something, you add it to the Queue.
Edit: Example code
import threading
import Queue
import time
def f(q):
while True:
print q.get() #block thread until something shows up
q = Queue.Queue()
t = threading.Thread(target=f,args=[q])
t.daemon = True #if parent dies, kill thread
t.start()
for x in range(0,1000):
q.put(x)
time.sleep(1)
-
【转载】tkinter多线程防假死
2020-08-16 09:43:48我们写的程序任务,可以分为CPU密集型和I/O密集型,CPU密集型是指需要大量消耗CPU资源进行密集计算的任务,比如浮点数计算,科学数据计算等,由于python有全局锁 (GIL)的存在,python多线程并不适合跑这种任务,因为...【转载链接】https://www.pythonf.cn/read/46694
我们写的程序任务,可以分为CPU密集型和I/O密集型,CPU密集型是指需要大量消耗CPU资源进行密集计算的任务,比如浮点数计算,科学数据计算等,由于python有全局锁 (GIL)的存在,python多线程并不适合跑这种任务,因为全局锁会限制所有的多线程其实是跑在一个主线程之下的,而多线程的线程切换是需要消耗CPU资源的,所以多线程跑CPU密集型不敢说是灾难,至少也快不起来。
解释下I/O密集型是啥情况,比如我们读写文件,从网上下载资源等都是I/O操作,I/O操作占用CPU资源往往不多,但是却挺费时间的,比如下载10个文件,你要是单线程一个一个下,一个在下,其他都得干等着,那时间就挺长的,碰到中间某一个下载比较慢,那就是浪费很多时间;多线程就是可以解决这个问题。一次3-4个文件同时下,一个没速度,闲置的带宽会被其他三个所瓜分,保证网络不会闲置,文件下载不会停止。
-
关于python tkinter 多线程依然无响应问题
2018-10-31 17:25:43今天解决了一个GUI程序的多线程问题。 因为GUI程序在执行高IO操作的时候容易出现假死和无响应的状态,所以需要用到多线程。 但我的程序开了线程之后依然是无响应状态。几次尝试,终于找到问题所在。 1.首先,我的...今天解决了一个GUI程序的多线程问题。
因为GUI程序在执行高IO操作的时候容易出现假死和无响应的状态,所以需要用到多线程。
但我的程序开了线程之后依然是无响应状态。几次尝试,终于找到问题所在。
1.首先,我的程序中有一个按钮button,点击之后运行函数self.starting
self.my_button = tk.Button(root, text="确定",command= self.starting)
其中self.starting用于启动线程,代码如下:
def starting(self): self.thread = threading.Thread(target=self.download) self.thread.setDaemon(True) self.thread.start()
2.线程中的target为self.downoad是一个自己定义的函数,用于执行某些功能,一开始的时候我的代码是这样写的
self.thread = threading.Thread(target=self.download(data))
因为download函数需要一个参数,所以我就想当然的将参数写在了创建线程函数中,但是发现依然是卡死,后来我就把参数data删了,用其他的方法传递参数,然后就不再出现卡死现象,至于原因我还没仔细的查过。
可以用一个简单的输出函数来测试一下:
def download(self): print("data"); time.sleep(5);
-
python tkinter多线程程序中listbox和scrollbar的问题
2017-05-13 03:27:42最近在使用tkinter写的一个多线程的程序的时候遇到一个问题。 这个程序有5个线程,每个线程都会去执行show函数,show函数是输出0至999到listbox上。 我在当线程要输出数据到listbox上时使用了线程锁,可是... -
tkinter多线程的应用(临时版).py
2019-01-23 17:28:51from tkinter import * import datetime import threading def do_word(): # 这里没特别的需求不需要动 global a if a: t = threading.Thread(target=task) t.setDaemon(True) t.start()... -
Python GUI之tkinter 实战(二)tkinter+多线程
2017-12-21 00:23:02Python3 tkinter系列 一、概述 二、布局管理 三、常用组件 四、实战(一) 五、实战(二) ...如果需要创建多个窗口该怎么办呢?那就需要使用另一个控件——Toplevel 在第一篇概述的主要控件列表 -
tkinter 与多线程
2018-01-25 17:40:57在哪个线程里调用了tk.mainloop(),就只能在哪个线程里更新UI。 下例演示了如何更新。 import Tkinter as tk from ttk import * import time import Queue, threading class MainWindow: def __init -
tkinter+多线程+socket网络登录,群聊聊天室
2019-02-28 14:52:09tkinter+多线程+socket网络登录,群聊聊天室. 可账户,账户保存至本地文件 -
pythontk多线程_使用Tkinter的多线程python
2020-12-10 08:27:31width=400, height=300): self.width, self.height = width, height self.canvas = Tkinter.Canvas(width=width, height=height, bg='black') self.canvas.pack(fill='none', expand=False) self._oid = [] self.... -
python+Tkinter+多线程
2015-12-31 14:06:14界面和多线程一向是编程里比较难的地方,常见的做法一般是界面一个线程,后台新开一个工作线程,这两个线程进行通信,这样可以让界面不至于为响应。在python中可以利用队列完成整体的架构设计。直接给大家看代码吧,... -
pythontk多线程_Python tkinter多进程多线程前邮箱,再用pyinstaller编译成exe
2021-03-17 14:37:49写博客记录一下Python 用tkinter 多进程线程写成的邮箱应用,只是一个简单的应用,尝试进程调度,并用pyinstaller打包成exe注意:1、平台环境win7 、Python3.6 、 pyinstaller3.3(安装方法,百度有,这里不加说明,...