Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 属性错误:'_tkinter.tkapp';对象没有属性';标题栏';_Python_Multithreading_Tkinter - Fatal编程技术网

Python 属性错误:'_tkinter.tkapp';对象没有属性';标题栏';

Python 属性错误:'_tkinter.tkapp';对象没有属性';标题栏';,python,multithreading,tkinter,Python,Multithreading,Tkinter,我已经用tkinter编写了一个GUI。在后台,我必须进行一些密集的计算。有时,我想将一些信息从计算线程写入GUI窗口 首先,我认为将计算代码添加到“mainloop”中是个好主意。但是这不起作用,因为主循环负责保持GUI的反应性。维护它似乎不是个好主意 下面,我创建了一个虚拟应用程序,它对我的新想法进行了抨击。示例应用程序有一个容器。在那个容器里,有一个标题栏。下面定义了类标题栏。它只包含一个标签 接下来我定义一个简单的线程。它模拟了一个需要将一些信息写入GUI的耗时计算 import tki

我已经用tkinter编写了一个GUI。在后台,我必须进行一些密集的计算。有时,我想将一些信息从计算线程写入GUI窗口

首先,我认为将计算代码添加到“mainloop”中是个好主意。但是这不起作用,因为主循环负责保持GUI的反应性。维护它似乎不是个好主意

下面,我创建了一个虚拟应用程序,它对我的新想法进行了抨击。示例应用程序有一个容器。在那个容器里,有一个标题栏。下面定义了类标题栏。它只包含一个标签

接下来我定义一个简单的线程。它模拟了一个需要将一些信息写入GUI的耗时计算

import tkinter as tk
import threading
import time


class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        # initialize the main window
        tk.Tk.__init__(self, *args, **kwargs)

        # add a container which will take all the widgets
        container = tk.Frame(self, bg="green")
        container.pack(side="top", fill="both", expand=True)

        # Add a titlebar object (defined below)
        titleBar = TitleBar(container, controller=self) 
        titleBar.grid(row=0, column=0, columnspan=2, sticky=tk.N+tk.W+tk.E)


class TitleBar(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        # the title bar contains only one label element
        titleLabel = tk.Label(self, text="This is the initial text")
        titleLabel.pack(side=tk.LEFT)


# Define a thread that runs in the background to perform intensive calculations
class MyTestThread(threading.Thread):
    def run(self):
        for i in range(10):
            time.sleep(1)
            a = i+100        # intensive calculation

            # from time to time: inform use about status
            print(a)      # printing to console works fine
            app.titleBar.titleLabel['text'] = "test 1"   # --- FAILS ---


if __name__ == "__main__":
    app = SampleApp()

    app.titleBar.titleLabel['text'] = "test 2"   # --- FAILS ---

    t = MyTestThread()
    t.start()

    app.mainloop()
问题是我无法访问标签来向其写入信息。无论是从线程内部还是从应用程序本身,写入都会失败。在这两种情况下,它都会失败,并出现以下错误:

AttributeError: '_tkinter.tkapp' object has no attribute 'titleBar'
如何访问和更改标签对象的属性

多谢各位


Bernd

您忘了将
self
放在
标题栏
属性之前

在eyllanesc和Mark_Bassem的帮助下,我解决了这个问题。看来问题的确很简单

为了防止将来人们希望看到这篇文章,我在这里留下了正确的代码:

import tkinter as tk
import threading
import time


class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        # initialize the main window
        tk.Tk.__init__(self, *args, **kwargs)

        # add a container which will take all the widgets
        container = tk.Frame(self, bg="green")
        container.pack(side="top", fill="both", expand=True)

        # Add a titlebar object (defined below)
        self.titleBar = TitleBar(container, controller=self) 
        self.titleBar.grid(row=0, column=0, columnspan=2, sticky=tk.N+tk.W+tk.E)


class TitleBar(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        # the title bar contains only one label element
        self.titleLabel = tk.Label(self, text="This is the initial text")
        self.titleLabel.pack(side=tk.LEFT)


# Define a thread that runs in the background to perform intensive calculations
class MyTestThread(threading.Thread):
    def run(self):
        for i in range(10):
            time.sleep(1)
            a = i+100        # intensive calculation

            # from time to time: inform use about status
            print(a)      # printing to console works fine
            app.titleBar.titleLabel['text'] = "status: " + str(a)


if __name__ == "__main__":
    app = SampleApp()

    #app.titleBar.titleLabel['text'] = "test 2"

    t = MyTestThread()
    t.start()

    app.mainloop()

打字错误:标题栏不是SampleApp的属性,而标题栏中的标题栏不是标题栏的属性,如果您希望它变为:
titleBar=titleBar(…
self.titleBar=titleBar(…
titleLabel=tk.Label(…
self.titleLabel=tk.Label(…