Tkinter / Python:框架没有抬起

时间:2018-11-27 07:39:22

标签: python tkinter

您好,我想知道为什么在MainPage中按下“确定”按钮时,DisplayPage没有抬起。我已经截断了大部分代码(为了将其保持在错误点上,所以将其进行了细分,但基本上我正在创建2帧(MainPage和DisplayPage),并希望基本上让用户将数据输入到MainPage中然后按下一步按钮在DisplayPage中显示/输出输入数据。

这是创建意外结果的代码部分。

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

1 个答案:

答案 0 :(得分:0)

您必须创建一个顶层窗口。我已经修改了您的代码,但只添加了最必要的代码。您也可以将小部件放在顶层窗口中(请参见标签)。

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