0

Hello I've set up a GUI for my Python Pinger but somehow I'm unable to change label placed inside the Main Class of the program.
Here is the main script:

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()

And here is the support script:

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()

When I try to change the label I get "type object 'PythonPingIT' has no attribute 'Motol'". So I thought that maybe I need to call the class like this:

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

but only got this

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")

I am really lost and if anyone knows how to solve this, I would be really grateful

EDIT:// I managed to access the value with this code:

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

But I still am unable to change the text, even with w.Motol.pack() or w.update()

johnyxmd
  • 1
  • 2
  • You didn't provide the argument `top` to `PythonPingIT()` in the line `p.PythonPingIT().Motol.configure(text="hey")`, so `top` will be None inside `__init__()` of `PythonPingIT` class. – acw1668 May 21 '20 at 09:56
  • When I try it this way `p.PythonPingIT(top).Motol.configure(text='''FU''')`, I just get new window, but the label is the same – johnyxmd May 21 '20 at 10:20
  • ***`import PyngIT as p`***: That's wrong, `PyngIT` is a module but you need the reference `root` from `top = PythonPingIT (root)`. Furthermore, this should read: `.Motol....` – stovfl May 21 '20 at 10:33
  • Does this answer your question? [how-would-i-access-variables-from-one-class-to-another](https://stackoverflow.com/questions/19993795) – stovfl May 21 '20 at 10:34
  • Well I just need to update the text at self.Motol() to a different text. I updated the post as I've managed to access it, but can't change it... If someone could show me a solution that would be great – johnyxmd May 21 '20 at 10:37

0 Answers0