0

With the example from the post here, i created a switching frame app. When i used StringVar as a variable inside the Frame page i modified as the code below, the UI doesnt show up, the code is running in background

from tkinter import *
from tkinter import font  as tkfont
import time
import threading

class RMS_APP(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
        self.title("Restaurant Management System")
        self.master = Frame(self)
        self.master.grid(row=0,column=0,sticky='nwes')
        #self.geometry("1600x800+0+0")

        #self.master.value = StringVar()
        self.master.grid_rowconfigure(0, weight=1)
        self.master.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (LogIn,):#,Kitchen):
            page_name = F.__name__
            frame = F(parent=self.master, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("LogIn")

    def show_frame(self, page_name,arg = None):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]


        if arg:
            #frame.OnClick(arg)
            #self.label1.grid_forget()
            frame.label1['text'] = arg
            pass
        frame.tkraise()

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

        self.localtime=time.asctime(time.localtime(time.time()))
        label = Label(self, text="Restaurant Management System", font=controller.title_font)
        label.grid(row=0,column=0,sticky='news',columnspan=3,padx = 500,pady=(150,10))
        lblinfo = Label(self, font=controller.title_font,text=self.localtime,fg="black",anchor=W)
        lblinfo.grid(row=1,column=0,columnspan=3,padx=500,pady=(0,10))
        button1 = Button(self, text="Table",height = 10, width = 15,
                            command=lambda: controller.show_frame("Table"))
        button2 = Button(self, text="Kitchen",height = 10, width = 15,
                            command=lambda: controller.show_frame("Kitchen"))
        button3 = Button(self, text="Bill Table",height = 10, width = 15,
                            command=lambda: controller.show_frame("Kitchen"))
        button1.grid(row=2,column=0,sticky='news',padx=(250,10),pady = (100,10))
        button2.grid(row=2,column=1,sticky='news',padx=(0,10),pady = (100,10))
        button3.grid(row=2,column=2,sticky='news',pady = (100,10))
if __name__ == "__main__":
    app = RMS_APP()
    app.mainloop()

When i un-comment the line self.master.value the code is not starting.by commenting it i am able to start the code, and it is displaying the LogIn page.Is there anything else i need to check

Myjab
  • 924
  • 1
  • 7
  • 22
  • `self.master.value` implies that the Tk obejct (_app_) has a function `value`, which is not the case. Set it to just a variable and not the `master` – FrainBr33z3 May 08 '19 at 11:13

1 Answers1

1

I have tried to find the problem by reducing the code but been unable to find anything. This code will not show a window if I uncomment the StringVar line:

from tkinter import *

class RMS_APP(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.master = Frame(self)

        self.my_integer = 4     # Works!
        #self.my_stringvar = StringVar()    # Works not!

RMS_APP().mainloop()

But if I inherit my class from Frame it works:

class RMS_APP(Frame):

I don't know why this is...

Update Both Tk and Frame already have an attribute 'master'. And RMS_APP.master == '.'. When you change this apparently things break.

figbeam
  • 7,001
  • 2
  • 12
  • 18
  • 1
    Rename ```self.master``` to anything else and it will work. ```tk.Tk``` has an internal attribute with the same name – Joshua Nixon May 08 '19 at 11:40
  • I can't figure it out either it is very weird. – Saad May 08 '19 at 11:40
  • 1
    @JoshuaNixon: oh I see now, it is because `self.master` is an internal attribute and we shouldn't change it as the child widget depends on `master` of `Tk` window. – Saad May 08 '19 at 11:43
  • So silly that was the worst part ever. Thanks for the help guys. – Myjab May 09 '19 at 12:41