來自回調 tkinter 的多處理
問題是當我想從回呼 tkinter(特別是函數 tkinter 的綁定)觸發進程「多重處理」時。錯誤從這裡開始:
file“c:\users\usuario\downloads\engine_controller_ls\extracttext\funciones\reproductor.py”,第598 行,在__init__ p.start() 檔案「c:\program files\python311\ lib\multiprocessing\process .py”,第121 行,開始self._popen = self._popen(self) ^^^^^^^^^^^^^^^^^^ 檔案“c:\program files\ python311\lib\multiprocessing\ context.py”,第224 行,在_popen 中返回_default_context.get_context().process._popen(process_obj) ^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ files\python311\lib\multiprocessing\context.py”,第336 行,在_popen 返回popen(process_obj) ^^^^^^^^^^^^^^^^^^^ 文件"c:\program files\python311\lib\multiprocessing\popen_spawn_win32.py",第95 行,在__init__duction.dump(process_obj, to_child) 檔案「c:\program files\python311\lib\mul, to_child) 檔案「c:\program files\python311\lib\mul, to_child) 檔案「c:\program files\python311\lib\mul. ,在轉儲forkingpickler(file, protocol).dump(obj) 中typeerror: 無法pickle ' _tkinter.tkapp'物件
main.py
from reproductor import mediaplayer def item_selected(event): """ evento invocado cuando el contenido de una carpeta es abierto. """ item_seleccionado = arbol.selection() item = arbol.item(item_seleccionado) print(item) #extraer la ruta del archivo y si encuentra espacios unirlos ruta = "" for element in item["values"]: ruta += element + " " #iniciacion del video global reproductor_video # cerrar el video para que no se abra en multiples ventanas if reproductor_video is not none: reproductor_video.closeplayer() reproductor_video = mediaplayer(ruta, frame_visualizer, frame_botones_procesar, spininicio=inbox_inicio, spinfinal=inbox_fin, spinactual=inbox_actual, mainvideo=true) reproductor_video.update_progres_video() arbol = checkboxtreeview(frame_tree_in) arbol.bind("<<treeviewselect>>", item_selected)
reproductor.py
class MediaPlayer: def __init__(ruta, frame_visualizer, frame_botones_procesar, spinInicio=inBox_inicio, spinFinal=inBox_fin, spinActual=inBox_Actual, mainVideo=True): p = Process(target=self.funcion1) p.start() #p.join def funcion1(self): cont = 0 while cont < 100: cont += 1 print("contador =", cont)
我在主函數中嘗試了相同的程式碼,並且它執行得很好,但是從回調中它不會執行。
正確答案
原版
#解決方案在這裡為需要它的人提供,實際上,如果它可以從 tkinter 上的事件啟動,如下所示:
翻譯(Google翻譯):
解決方案在這裡為需要它的人提供,事實上它可以從 tkinter 中的事件啟動,如下所示:
from multiprocessing import Process, log_to_stderr, get_logger import time from tkinter import Tk from tkinter import ttk import logging import sys def item_selected(event): my_objeto = objeto() button_eraser.config(command=lambda:my_objeto.lanzador(2)) button_marco.config(command=lambda:my_objeto.lanzador(1)) my_objeto.start_player() class objeto: def __init__(self) -> None: #self.start_player() pass def start_player(self): pass def funcion1(self): contador = 0 while 1: contador += 1 print("Func1: ", contador, self.palabra) time.sleep(0.1) if contador > 80: break q = Process(target=self.funcion3) q.start() #q.join() def funcion2(self): contador = 0 while 1: contador += 2 print("Func2: ", contador) time.sleep(0.2) if contador > 120: break def funcion3(self): contador = 0 while 1: contador += 2 print("Func3: ", contador) time.sleep(0.02) if contador > 200: break def lanzador(self,mode): log_to_stderr(logging.DEBUG) logger = get_logger() logger.setLevel(logging.INFO) if mode == 1: self.p = Process(target=self.funcion1) self.p.start() elif mode == 2: self.p = Process(target=self.funcion2) self.p.start() def unir(self): self.p.join() print("Salio") def iniciar_multis(self): self.lanzador(2) self.lanzador(1)
以上是來自回調 tkinter 的多處理的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。Python以简洁和强大的生态系统著称,C 则以高性能和底层控制能力闻名。

Python在遊戲和GUI開發中表現出色。 1)遊戲開發使用Pygame,提供繪圖、音頻等功能,適合創建2D遊戲。 2)GUI開發可選擇Tkinter或PyQt,Tkinter簡單易用,PyQt功能豐富,適合專業開發。

Python更易學且易用,C 則更強大但複雜。 1.Python語法簡潔,適合初學者,動態類型和自動內存管理使其易用,但可能導致運行時錯誤。 2.C 提供低級控制和高級特性,適合高性能應用,但學習門檻高,需手動管理內存和類型安全。

要在有限的時間內最大化學習Python的效率,可以使用Python的datetime、time和schedule模塊。 1.datetime模塊用於記錄和規劃學習時間。 2.time模塊幫助設置學習和休息時間。 3.schedule模塊自動化安排每週學習任務。

Python在開發效率上優於C ,但C 在執行性能上更高。 1.Python的簡潔語法和豐富庫提高開發效率。 2.C 的編譯型特性和硬件控制提升執行性能。選擇時需根據項目需求權衡開發速度與執行效率。

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

Python在自動化、腳本編寫和任務管理中表現出色。 1)自動化:通過標準庫如os、shutil實現文件備份。 2)腳本編寫:使用psutil庫監控系統資源。 3)任務管理:利用schedule庫調度任務。 Python的易用性和豐富庫支持使其在這些領域中成為首選工具。

每天學習Python兩個小時是否足夠?這取決於你的目標和學習方法。 1)制定清晰的學習計劃,2)選擇合適的學習資源和方法,3)動手實踐和復習鞏固,可以在這段時間內逐步掌握Python的基本知識和高級功能。
