0

Hi I was wondering why my DisplayPage wasn't lifting when the ok button was pressed in MainPage. I've cut off most of my code (to keep it more to the point of the error, so its a bit segmented but basically I'm creating 2 frames (MainPage and DisplayPage) and want to basically let the user input data into MainPage and then press the next button to show/output the input data in DisplayPage.

Here's a section of the code that's creating the unexpected result.

from Tkinter import *
import Tkinter as tk
import os

class page(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
    def show(self):
        self.lift()

class DisplayPage(page):
    def __init__(self, *args, **kwargs):
        page.__init__(self, *args, **kwargs)
        # deleted - however will upload if needed more information about my code

class MainPage(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        p1 = DisplayPage(self)

# create all of the main containers 
        frame_A = Frame(self, width=930, height=780)
        frame_B = Frame(self, width=465, height=280)
        frame_C = Frame(self, width=465, height=280)
        frame_D = Frame(self, width=465, height=140)
        frame_E = Frame(self, width=465, height=70)

        # layout all of the main containers

        frame_A.grid(row=0, column=0, columnspan=2, rowspan=3)
        frame_B.grid(row=0, column=3)
        frame_C.grid(row=1, column=3)
        frame_D.grid(row=2, column=3)
        frame_E.grid(row=3, column=3)

        # next ok button

        content4= Frame(frame_E)
        ok = tk.Button(content4, text="Locate", font =('Roboto Thin', 30), command= p1.lift)
        ok.pack()

        # layout all widgets

        content1.grid(column=0, row=0)
        content2.grid(column=3, row=1)
        content3.grid(column=3, row=2)
        content4.grid(column=3, row=3)

        namelbl.grid(column=3, row=1)
        name.grid(column=3, row=6)
        namelbl2.grid(column=3, row=5)
        name2.grid(column=3, row=8)
        namelbl3.grid(column=3, row=7)
        name3.grid(column=3, row=10)
        one.grid(column=3, row=2)
        two.grid(column=3, row=3)
        three.grid(column=3, row=4)
        ok.grid(column=3, row=11)
        namelbl4.grid(column=3, row=9) 

        p1.show()

if __name__ == "__main__":
    root = tk.Tk()
    main = MainPage(root)
    main.pack(side="top", fill="both", expand=True)
    root.wm_title('MobilePark Simulator')
    root.wm_geometry("1300x830")
    root.mainloop()  
CommonSense
  • 4,232
  • 2
  • 14
  • 38
  • Maybe this may be interesting to you: https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter/7557028#7557028 – kantal Nov 29 '18 at 17:55

1 Answers1

0

You must create a toplevel window. I have modified your code, but only put the most necessary into it. You can place widgets in a toplevel window, too (see the label).

import Tkinter as tk
import os

class MainPage(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.p1flag=0           # EDIT1
        self.p1=tk.Toplevel(self)
        self.p1.title("Hello")
        self.p1label=tk.Label(self.p1,text="FYI "*20)
        self.p1label.pack()

# create all of the main containers 
        frame_A = tk.Frame(self, width=930, height=780)
        frame_B = tk.Frame(self, width=465, height=280)
        frame_C = tk.Frame(self, width=465, height=280)
        frame_D = tk.Frame(self, width=465, height=140)
        frame_E = tk.Frame(self, width=465, height=70)

        frame_A.grid(row=0, column=0, columnspan=2, rowspan=3)
        frame_B.grid(row=0, column=3)
        frame_C.grid(row=1, column=3)
        frame_D.grid(row=2, column=3)
        frame_E.grid(row=3, column=3)

        content4= tk.Frame(frame_E)
        self.ok = tk.Button(content4, text="Locate", font =('Roboto Thin', 30), command= self.show_p1)  # EDIT2
        self.ok.pack()

        # layout all widgets
        content4.grid(column=3, row=3)



    def show_p1(self):   # EDIT3
        if not self.p1flag%2:
            self.p1.lift()
        else:
        self.p1.lower()
    self.p1flag+=1


if __name__ == "__main__":
    root = tk.Tk()
    main = MainPage(root)
    main.pack(side="top", fill="both", expand=True)
    root.wm_title('MobilePark Simulator')
    root.wm_geometry("1200x900")
    root.mainloop()  
kantal
  • 2,331
  • 2
  • 8
  • 15
  • nvm fixed - Thanks @kantal! – Jasper Yip Nov 28 '18 at 02:40
  • right sorry haha, @kantal I was also wondering if you knew how to make it so that once I press the locate / or "ok" button in this case, that the frame hides itself? – Jasper Yip Nov 28 '18 at 12:11
  • @Jasper Yip I've changed the code in 3 places. lift() brings the subwindow (or top level) window to the fore, lower() puts it in the background. – kantal Nov 28 '18 at 15:26