나뿐만 아니라 문제 풀의 외부 Tkinter의 인스턴스를 생성하고, 클래스 인스턴스가 발생하고 별도의 프로세스에서 함수를 호출하지 않은 감사드립니다. "간단한"예제는 다음과 같습니다.
from multiprocessing import Process
import time
try:
import Tkinter as tk ## Python 2.x
except:
import tkinter as tk ## Python 3.x
class ProgressBar():
def __init__(self, root):
self.root=root
self.root.geometry("75x50+900+100")
self.ctr=25
def mainloop(self):
self.root.mainloop()
def start_countdown(self):
""" a separate process in a separate GUI
"""
self.root.withdraw()
self.top_count=tk.Toplevel(self.root)
self.top_count.geometry("75x50+750+50")
self.label_ctr = tk.IntVar()
self.label_ctr.set(self.ctr)
label = tk.Label(self.top_count, textvariable=self.label_ctr)
label.pack()
if self.ctr > 0:
self.top_count.after(750, self.update)
def start_running(self):
""" create the progress bar widget
"""
self.top=tk.Toplevel(self.root, takefocus=True)
self.top.title("Progress Bar")
self.top.geometry("+700+200")
canvas = tk.Canvas(self.top, width=261, height=60, bg='lightgray')
canvas.pack()
rc2 = canvas.create_rectangle(15, 20, 243, 50, outline='blue', \
fill='lightblue')
rc1 = canvas.create_rectangle(24, 20, 34, 50, outline='white', \
fill='blue')
total=100
x = 5
while self.ctr: ## move the small rectangle +5 or -5 units
total += x
if total > 311:
x = -5
elif total < 100:
x = 5
time.sleep(0.2)
canvas.move(rc1, x, 0)
canvas.update()
def update(self):
self.ctr -= 1
self.label_ctr.set(self.ctr)
if self.ctr > 0:
self.top_count.after(750, self.update)
else:
## sleep to allow any remaining after() to execute
## can also use self.root.after_cancel(id)
self.top_count.after(500, self.root.destroy) ## destroy root when zero is reached
root = tk.Tk()
PB=ProgressBar(root)
pr1=Process(target=PB.start_countdown(), args=())
pr1.start()
pr2=Process(target=PB.start_running(), args=())
pr2.start()
## start mainloop in a separate process as a function of the class
## don't know if this is really necessary or not
## the theory is, it is detached from the other 2 processes and so
## can react to both independently
## also the mainloop() process can be killed=shut down properly
pr3=Process(target=PB.mainloop(), args=())
pr3.start()
## safety clean up
pr1.terminate()
pr2.terminate()
pr3.terminate()
프로세스 경계에서 위젯을 전달할 수 없습니다. –
그 냄새. 내 생각이 간다. –