简体   繁体   English

python pack()和grid()方法在一起

[英]python pack() and grid() methods together

Im new to python so please forgive my Noob-ness. 我是python的新手所以请原谅我的Noob-ness。 Im trying to create a status bar at the bottom of my app window, but it seems every time I use the pack() and grid() methods together in the same file, the main app window doesn't open. 我试图在我的应用程序窗口底部创建一个状态栏,但似乎每次我在同一个文件中一起使用pack()和grid()方法时,主应用程序窗口都不会打开。 When I comment out the line that says statusbar.pack(side = BOTTOM, fill = X) my app window opens up fine but if I leave it in it doesn't, and also if I comment out any lines that use the grid method the window opens with the status bar. 当我注释掉说明statusbar.pack(side = BOTTOM,fill = X)的行时,我的应用程序窗口打开正常,但是如果我把它留在里面就不行,而且如果我注释掉任何使用网格方法的行窗口打开,状态栏。 It seems like I can only use either pack() or grid() but not both. 看起来我只能使用pack()或grid(),但不能同时使用两者。 I know I should be able to use both methods. 我知道我应该可以使用这两种方法。 Any suggestions? 有什么建议么? Here's the code: 这是代码:

from Tkinter import *
import tkMessageBox

def Quit():
 answer = tkMessageBox.askokcancel('Quit', 'Are you sure?')
 if answer:
    app.destroy()

app = Tk()
app.geometry('700x500+400+200')
app.title('Title')

label_1 = Label(text = "Enter number")
label_1.grid(row = 0, column = 0)
text_box1 = DoubleVar() 
input1 = Entry(app, textvariable = text_box1)
input1.grid(row = 0, column = 2)

statusbar = Label(app, text = "", bd = 1, relief = SUNKEN, anchor = W)
statusbar.pack(side = BOTTOM, fill = X)

startButton = Button(app, text = "Start", command = StoreValues).grid(row = 9, column = 2,  padx = 15, pady = 15)

app.mainloop() 

Any help is appreciated! 任何帮助表示赞赏! Thanks! 谢谢!

You cannot use both pack and grid on widgets that have the same master. 您不能在具有相同主控的小部件上同时使用packgrid The first one will adjust the size of the widget. 第一个将调整小部件的大小。 The other will see the change, and resize everything to fit it's own constraints. 另一个将看到变化,并调整一切以适应它自己的约束。 The first will see these changes and resize everything again to fit its constraints. 第一个将看到这些更改并再次调整所有内容以适应约束。 The other will see the changes, and so on ad infinitum. 另一个将无限制地看到变化等等。 They will be stuck in an eternal struggle for supremacy. 他们将陷入永恒的争夺至高无上的斗争中。

While it is technically possible if you really, really know what you're doing, for all intents and purposes you can't mix them in the same container . 虽然技术上可行,如果你真的, 真的知道你正在做什么,无论出于各种意图和目的,你都不能将它们混合在同一个容器中 You can mix them all you want in your app as a whole, but for a given container (typically, a frame), you can use only one to manage the direct contents of the container. 您可以在应用程序中将它们全部混合在一起,但对于给定的容器(通常是一个框架),您只能使用一个来管理容器的直接内容。

A very common technique is to divide your GUI into pieces. 一种非常常见的技术是将GUI分成几部分。 In your case you have a bottom statusbar, and a top "main" area. 在您的情况下,您有一个底部状态栏和一个顶部“主”区域。 So, pack the statusbar along the bottom and create a frame that you pack above it for the main part of the GUI. 因此,将状态栏打包在底部并创建一个框架,在其上方为GUI的主要部分打包。 Then, everything else has the main frame as its parent, and inside that frame you can use grid or pack or whatever you want. 然后,其他一切都将主框架作为其父框架,并在该框架内,您可以使用网格或包或任何您想要的。

Yeah thats right. 是啊,没错。 In following example, i have divided my program into 2 frames. 在下面的示例中,我将程序划分为2帧。 frame1 caters towards menu/toolbar and uses pack() methods wherein frame2 is used to make login page credentials and uses grid() methods. frame1迎合菜单/工具栏并使用pack()方法,其中frame2用于制作登录页面凭据并使用grid()方法。

from tkinter import *

def donothing():
    print ('IT WORKED')
root=Tk()
root.title(string='LOGIN PAGE')

frame1=Frame(root)
frame1.pack(side=TOP,fill=X)

frame2=Frame(root)
frame2.pack(side=TOP, fill=X)

m=Menu(frame1)
root.config(menu=m)

submenu=Menu(m)
m.add_cascade(label='File',menu=submenu)
submenu.add_command(label='New File', command=donothing)
submenu.add_command(label='Open', command=donothing)
submenu.add_separator()
submenu.add_command(label='Exit', command=frame1.quit)


editmenu=Menu(m)
m.add_cascade(label='Edit', menu=editmenu)
editmenu.add_command(label='Cut',command=donothing)
editmenu.add_command(label='Copy',command=donothing)
editmenu.add_command(label='Paste',command=donothing)
editmenu.add_separator()
editmenu.add_command(label='Exit', command=frame1.quit)


# **** ToolBar *******

toolbar=Frame(frame1,bg='grey')
toolbar.pack(side=TOP,fill=X)
btn1=Button(toolbar, text='Print', command=donothing)
btn2=Button(toolbar, text='Paste', command=donothing)
btn3=Button(toolbar, text='Cut', command=donothing)
btn4=Button(toolbar, text='Copy', command=donothing)
btn1.pack(side=LEFT,padx=2)
btn2.pack(side=LEFT,padx=2)
btn3.pack(side=LEFT,padx=2)
btn4.pack(side=LEFT,padx=2)

# ***** LOGIN CREDENTIALS ******
label=Label(frame2,text='WELCOME TO MY PAGE',fg='red',bg='white')
label.grid(row=3,column=1)

label1=Label(frame2,text='Name')
label2=Label(frame2,text='Password')
label1.grid(row=4,column=0,sticky=E)
label2.grid(row=5,column=0,sticky=E)

entry1=Entry(frame2)
entry2=Entry(frame2)
entry1.grid(row=4,column=1)
entry2.grid(row=5,column=1)

chk=Checkbutton(frame2,text='KEEP ME LOGGED IN')
chk.grid(row=6,column=1)

btn=Button(frame2,text='SUBMIT')
btn.grid(row=7,column=1)




# **** StatusBar ******************

status= Label(root,text='Loading',bd=1,relief=SUNKEN,anchor=W)
status.pack(side=BOTTOM, fill=X)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM