2015-11-24 25 views
0

经过多年的阅读,这是我第一篇文章Stack Overflow。通过过程中的方法更新TKInter标签

我正在写一个Python程序并用TKInter构建一个前端。我的课堂上有一个名为label_cpu的标签。我的希望是,我可以在进程中使用方法cpu_counter来更新当前CPU使用时间的标签。如果我把一个实例的方法,说GUI.cpu_counter()标签的更新,但是当我试图通过的Process.Start(启动方法)谈到了以下消息:

Traceback (most recent call last): 
    File "C:/Users/Chris Local/OneDrive/Github/python/gui2.py", line 51, in <module> 
    p.start() 
    File "C:\Python35\lib\multiprocessing\process.py", line 105, in start 
    self._popen = self._Popen(self) 
    File "C:\Python35\lib\multiprocessing\context.py", line 212, in _Popen 
    return _default_context.get_context().Process._Popen(process_obj) 
    File "C:\Python35\lib\multiprocessing\context.py", line 313, in _Popen 
    return Popen(process_obj) 
    File "C:\Python35\lib\multiprocessing\popen_spawn_win32.py", line 66, in __init__ 
    reduction.dump(process_obj, to_child) 
    File "C:\Python35\lib\multiprocessing\reduction.py", line 59, in dump 
    ForkingPickler(file, protocol).dump(obj) 
_pickle.PicklingError: Can't pickle <class '_tkinter.tkapp'>: attribute lookup tkapp on _tkinter failed 

Process finished with exit code 1 

这里的验证码:

from tkinter import * 
import thread 
import sys 
import os 
from multiprocessing import Process 
from tkinter import Tk, Checkbutton, Label 
from tkinter import StringVar, IntVar 

class MainMenu: 
    def __init__(self, master): 
     frame = Frame(master) 
     frame.pack() 
     # Configure label for CPU counter/thread 
     self.m = StringVar() 
     self.m.set("CPU COUNTER GOES HERE") 
     self.label_cpu = Label(frame, textvariable = self.m) 
     self.button1 = Button(frame, text = "1. Enter Provinces (from Scratch)") 
     self.label_cpu.grid(row=1, sticky = W) 
     self.button1.grid(row=2, sticky = W) 

    def cpu_counter(self): 
     while (1 > 0): 
      v = str(os.times()) 
      print(v) 
      self.m.set(v) 

def cpu_counter_external(GUI): 
    #I tried tricking out the process.start() by aiming it here, but 
    #this doesn't work if launched in a process either. 
    GUI.cpu_counter() 

if __name__ == '__main__': 
    root = Tk() 
    menubar = Menu(root) 
    menubar.option_add('*tearOff', FALSE) 
    submenu = Menu(menubar) 
    menubar.add_cascade(label = "File!", menu = submenu) 
    menubar.add_cascade(label = "Help!", menu = submenu) 
    root.config(menu=menubar) 
    GUI = MainMenu(root) 
    p = Process(target = GUI.cpu_counter) 

    #The following line, GUI.cpu_counter() updates the label 
    #but it should not be run unless in a process. 
    #GUI.cpu_counter() 

    p.start() 
    p.join() 
    root.mainloop() 

回答

1

你不能用这种方式与多使用Tkinter的。 Tkinter对象不能在进程之间共享。

0

如果我明白你想要做什么,多处理不是必须的。否则,请使用经理字典,请参阅Doug Hellman的Pymotw https://pymotw.com/2/multiprocessing/communication.html和网上其他地方的Managng Shared State和Shared Namespaces。如果您想了解更多相关信息,请使用经理字典发布新主题。

from tkinter import * 
import thread 
import sys 
import os 
from multiprocessing import Process 

## redundant as you already have import * 
##from tkinter import Tk, Checkbutton, Label 
##from tkinter import StringVar, IntVar 

class MainMenu: 
    def __init__(self, master): 
     self.master=master 
     frame = Frame(master) 
     frame.pack() 
     self.ctr=0 
     # Configure label for CPU counter/thread 
     self.m = StringVar() 
     self.m.set("CPU COUNTER GOES HERE") 
     self.label_cpu = Label(frame, textvariable = self.m) 
     self.button1 = Button(frame, text = "1. Enter Provinces (from Scratch)") 
     self.label_cpu.grid(row=1, sticky = W) 
     self.button1.grid(row=2, sticky = W) 
     self.cpu_counter() 

    def cpu_counter(self): 
     v = str(os.times()) 
     print(v, self.ctr) 
     self.m.set(v) 
     if self.ctr < 100: ## stop while testing 
      self.ctr += 1 
      self.master.after(100, self.cpu_counter) 

def cpu_counter_external(GUI): 
    #I tried tricking out the process.start() by aiming it here, but 
    #this doesn't work if launched in a process either. 
    GUI.cpu_counter() 

if __name__ == '__main__': 
    root = Tk() 
    menubar = Menu(root) 
    menubar.option_add('*tearOff', FALSE) 
    submenu = Menu(menubar) 
    menubar.add_cascade(label = "File!", menu = submenu) 
    menubar.add_cascade(label = "Help!", menu = submenu) 
    root.config(menu=menubar) 
    GUI = MainMenu(root) 
## p = Process(target = GUI.cpu_counter) 

    #The following line, GUI.cpu_counter() updates the label 
    #but it should not be run unless in a process. 
    #GUI.cpu_counter() 

## p.start() 
## p.join() 
    root.mainloop()