无法更改Tkinter中标签的文本

2024-06-08 04:32:19 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我已经为Python Pinger设置了一个GUI,但不知何故,我无法更改程序主类中的标签。
以下是主要脚本:

import sys

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    import ttk
    py3 = False
except ImportError:
    import tkinter.ttk as ttk
    py3 = True

import PyngIT_support

def vp_start_gui():
    '''Starting point when module is the main routine.'''
    global val, w, root
    root = tk.Tk()
    top = PythonPingIT (root)
    PyngIT_support.init(root, top)
    root.mainloop()

w = None
def create_PythonPingIT(rt, *args, **kwargs):
    '''Starting point when module is imported by another module.
       Correct form of call: 'create_PythonPingIT(root, *args, **kwargs)' .'''
    global w, w_win, root
    #rt = root
    root = rt
    w = tk.Toplevel (root)
    top = PythonPingIT (w)
    PyngIT_support.init(w, top, *args, **kwargs)
    return (w, top)

def destroy_PythonPingIT():
    global w
    w.destroy()
    w = None

class PythonPingIT:
    def __init__(self, top=None):
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9' # X11 color: 'gray85'
        _ana1color = '#d9d9d9' # X11 color: 'gray85'
        _ana2color = '#ececec' # Closest X11 color: 'gray92'
        self.style = ttk.Style()
        if sys.platform == "win32":
            self.style.theme_use('winnative')
        self.style.configure('.',background=_bgcolor)
        self.style.configure('.',foreground=_fgcolor)
        self.style.configure('.',font="TkDefaultFont")
        self.style.map('.',background=
            [('selected', _compcolor), ('active',_ana2color)])

        top.geometry("1200x800+373+87")
        top.minsize(176, 1)
        top.maxsize(1924, 1050)
        top.resizable(1, 1)
        top.title("PyngIT")
        top.configure(background="#e4e4e4")
        top.configure(highlightbackground="#d9d9d9")
        top.configure(highlightcolor="black")

        self.Frame_StaniceA = tk.LabelFrame(top)
        self.Frame_StaniceA.place(relx=0.006, rely=0.0, relheight=0.331
                , relwidth=0.985)
        self.Frame_StaniceA.configure(relief='groove')
        self.Frame_StaniceA.configure(foreground="black")
        self.Frame_StaniceA.configure(text='''Stanice A''')
        self.Frame_StaniceA.configure(background="#d9d9d9")
        self.Frame_StaniceA.configure(highlightbackground="#d9d9d9")
        self.Frame_StaniceA.configure(highlightcolor="black")

        self.Motol = tk.Label(self.Frame_StaniceA)
        self.Motol.place(relx=0.007, rely=0.083, height=31, width=95
                , bordermode='ignore')
        self.Motol.configure(activebackground="#f9f9f9")
        self.Motol.configure(activeforeground="black")
        self.Motol.configure(background="#d9d9d9")
        self.Motol.configure(disabledforeground="#a3a3a3")
        self.Motol.configure(foreground="#000000")
        self.Motol.configure(highlightbackground="#d9d9d9")
        self.Motol.configure(highlightcolor="black")
        self.Motol.configure(text='''N. Motol''')

if __name__ == '__main__':
    vp_start_gui()

以下是支持脚本:

import sys
import multiping
import os
from threading import Thread

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    import ttk
    py3 = False
except ImportError:
    import tkinter.ttk as ttk
    py3 = True

def init(top, gui, *args, **kwargs):
    global w, top_level, root
    w = gui
    top_level = top
    root = top

def destroy_window():
    # Function which closes the window.
    global top_level
    top_level.destroy()
    top_level = None

def ping_a_comm():
    t = Thread(target=ping_A)
    t.start()
    pass

def ping_A():
    import PyngIT as p
    os.system("ping 192.168.0.1")
    p.PythonPingIT.Motol.configure(text="TEXT")
    pass

if __name__ == '__main__':
    import PyngIT
    PyngIT.vp_start_gui()

当我尝试更改标签时,我得到“type object 'PythonPingIT' has no attribute 'Motol'”。 所以我想也许我需要这样称呼这个班级:

p.PythonPingIT().Motol.configure(text="text")

但只有这个

Exception has occurred: AttributeError
'NoneType' object has no attribute 'geometry'
  File "C:\Users\admin\Desktop\PythonPingIt\PyngIT.py", line 67, in __init__
    top.geometry("1200x800+373+87")
  File "C:\Users\admin\Desktop\PythonPingIt\PyngIT_support.py", line 45, in ping_A
    p.PythonPingIT().Motol.configure(text="hey")

我真的迷路了,如果有人知道如何解决这个问题,我将非常感激

EDIT://我使用以下代码访问了该值:

def ping_A():
    global w, top_level, root
    import PyngIT as p
    root = tk.Tk()
    w = p.PythonPingIT (root)
    os.system("ping localhost")
    w.Motol.configure(text="FUUUu")
    pass

但是我仍然无法更改文本,即使使用w.Motol.pack()w.update()


Tags: importselfconfiguretopdefasrootframe

热门问题