不能将框架放在顶部

时间:2017-11-01 18:59:00

标签: python tkinter

我正在尝试在一个顶层窗口中放置一个带有一堆小部件的框架,我已经为其创建了菜单。当我运行程序时,我得到两个单独的窗口(参见屏幕抓取)。

我想要的是在较大的“主”窗口内看到标签,输入框,按钮等。

Screenshot of actual result

以下是代码:

# Basic menubar with a few sub-menus

from tkinter import *
from tkinter import ttk


root = Tk()

root.option_add("tearOff", FALSE)

# ***************  SETUP THE CONTAINERS ***********
root.title("Menu Example")
root.geometry("600x300")

#frame = ttk.Frame(root, borderwidth = 5, relief = "sunken")

#frame.grid(row=0, column=0, sticky = (N,W,E,S))

# ***************************************************

# ****  Create the Menubar
win = Toplevel(root)
menubar = Menu(win)

appmenu = Menu(menubar, name='apple')


menubar.add_cascade(menu=appmenu)
appmenu.add_command(label='About My Application')
appmenu.add_separator()


win["menu"]=menubar

# Add some menus to the menubar
menu_file = Menu(menubar)
menu_edit = Menu(menubar)
menu_transform = Menu(menubar)

menubar.add_cascade(menu=menu_file, label="File") # cascade menu items add a menu to a menubar
menubar.add_cascade(menu=menu_edit, label="Edit")

# Add items to the menus
menu_file.add_command(label = "New") # command menu items add commands to a menu.
menu_file.add_command(label = "Open...")
menu_file.add_command(label = "Close")
menu_file.add_separator()
menu_file.add_command(label = "Export as ...")


menu_edit.add_command(label = "Copy")
menu_edit.add_command(label = "Paste")
menu_edit.add_cascade(menu=menu_transform, label="Transform")
menu_transform.add_command(label="group")
menu_transform.add_command(label="ungroup")
menu_transform.add_command(label="align")


#menu_edit.add_command(label = "Transform")




# *****************************************
frame = ttk.Frame(win, borderwidth = 5, relief = "sunken")
frame.grid(row=0, column=0, sticky = (N,W,E,S))

# ***************************************************

ipaddr = StringVar()
username = StringVar()
password = StringVar()

ipAddrLbl = ttk.Label(frame, text="Switch IP Address:")
ipAddrLbl.grid(column=0, row=1, sticky = W)

usernameLbl = ttk.Label(frame, text="User Name:")
usernameLbl.grid(column=0, row=2, sticky = W)

passwordLbl = ttk.Label(frame, text="Password:")
passwordLbl.grid(column=0, row=3, sticky = W)

ipaddrEntry = ttk.Entry(frame, textvariable="ipaddr")
ipaddrEntry.grid(column=1, row=1, sticky = W)

usernameEntry = ttk.Entry(frame, textvariable="username")
usernameEntry.grid(column=1, row=2, sticky = W)

passwordEntry = ttk.Entry(frame, textvariable="password", show = "*")
passwordEntry.grid(column=1, row=3, sticky = W)

loginButton = ttk.Button(frame, text = "Login")
loginButton.grid(column=0, row=4, sticky=W)



root.mainloop()

2 个答案:

答案 0 :(得分:0)

我认为,此问题的最佳答案是图标化根窗口,然后可以将Toplevel修改为您喜欢的内容。这样,当主循环仍在没有双tkinter实例的情况下运行时,根主窗口将被隐藏。在这里,对编辑的代码进行了一些微调,以便在关闭Toplevel时停止主循环。

# Basic menubar with a few sub-menus

from tkinter import *
from tkinter import ttk

def _delete_window():
    print ("delete_window")
    try:
        root.destroy()
    except:
        pass

root = Tk()

root.option_add("tearOff", FALSE)

# ***************  SETUP THE CONTAINERS ***********
root.title("Menu Example")
root.geometry("600x300")
root.iconify()
#frame = ttk.Frame(root, borderwidth = 5, relief = "sunken")

#frame.grid(row=0, column=0, sticky = (N,W,E,S))

# ***************************************************

# ****  Create the Menubar
win = Toplevel(root)
win.geometry('400x150')
menubar = Menu(win)

appmenu = Menu(menubar, name='apple')


menubar.add_cascade(menu=appmenu)
appmenu.add_command(label='About My Application')
appmenu.add_separator()


win["menu"]=menubar

# Add some menus to the menubar
menu_file = Menu(menubar)
menu_edit = Menu(menubar)
menu_transform = Menu(menubar)

menubar.add_cascade(menu=menu_file, label="File") # cascade menu items add a menu to 
a menubar
menubar.add_cascade(menu=menu_edit, label="Edit")

# Add items to the menus
menu_file.add_command(label = "New") # command menu items add commands to a menu.
menu_file.add_command(label = "Open...")
menu_file.add_command(label = "Close")
menu_file.add_separator()
menu_file.add_command(label = "Export as ...")


menu_edit.add_command(label = "Copy")
menu_edit.add_command(label = "Paste")
menu_edit.add_cascade(menu=menu_transform, label="Transform")
menu_transform.add_command(label="group")
menu_transform.add_command(label="ungroup")
menu_transform.add_command(label="align")


#menu_edit.add_command(label = "Transform")




# *****************************************
frame = Frame(win, borderwidth = 5, relief = "sunken")
frame.pack()
frame.place(relx=0.5, rely=0.5, anchor=CENTER)
# ***************************************************

ipaddr = StringVar()
username = StringVar()
password = StringVar()

ipAddrLbl = ttk.Label(frame, text="Switch IP Address:")
ipAddrLbl.grid(column=0, row=1, sticky = W)

usernameLbl = ttk.Label(frame, text="User Name:")
usernameLbl.grid(column=0, row=2, sticky = W)

passwordLbl = ttk.Label(frame, text="Password:")
passwordLbl.grid(column=0, row=3, sticky = W)

ipaddrEntry = ttk.Entry(frame, textvariable="ipaddr")
ipaddrEntry.grid(column=1, row=1, sticky = W)

usernameEntry = ttk.Entry(frame, textvariable="username")
usernameEntry.grid(column=1, row=2, sticky = W)

passwordEntry = ttk.Entry(frame, textvariable="password", show = "*")
passwordEntry.grid(column=1, row=3, sticky = W)

loginButton = ttk.Button(frame, text = "Login")
loginButton.grid(column=0, row=4, sticky=W)

win.protocol("WM_DELETE_WINDOW", _delete_window)
root.mainloop()

答案 1 :(得分:-1)

更正了使用Toplevel小部件代替新Tk实例的代码:

import tkinter as tk
class Gui1:
    def __init__(self, root):
        self.root = root
        self.frame = tk.Frame(root)
        self.button =  tk.Button(text="Openwindow2",
        command=self.create_gui2)
        self.button.grid(row=0, column=0)
        self.frame.grid(row=1, column=1)

    def create_gui2(self):
        class Gui2:
            def __init__(self, root2):
                self.root2 = root2
                self.frame2 = tk.Frame(root2)
                self.label=tk.Label(text="Window 2")
                self.label.grid(row=0,column=0)
                self.frame2.grid(row=3,column=3)

        root2 = tk.Toplevel()
        app2 = Gui2(root2)


root = tk.Tk()
app = Gui1(root)
root.mainloop()