python pack()和grid()方法一起使用 [英] python pack() and grid() methods together

查看:1067
本文介绍了python pack()和grid()方法一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的python所以请原谅我的Noob-ness。我试图在我的应用程序窗口底部创建一个状态栏,但似乎每次我在同一个文件中一起使用pack()和grid()方法时,主应用程序窗口无法打开。当我注释掉表示statusbar.pack(side = BOTTOM,fill = X)的行时,我的应用程序窗口打开状态良好,但是如果我将它放在它中,并且如果我注释掉任何使用网格方法的行该窗口将打开状态栏。看来我只能使用pack()或grid(),但不能同时使用。我知道我应该可以使用这两种方法。有什么建议么?这里是代码:

 从Tkinter导入* 
导入tkMessageBox

def Quit() :
answer = tkMessageBox.askokcancel('Quit','Are you sure?')
如果答案:
app.destroy()

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

label_1 =标签(text =输入数字)
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)
$ b $ startButton = Button(app,text =Start,command = StoreValues).grid(row = 9,column = 2,padx = 15,pady = 15)

app.mainloop()

任何帮助表示感谢!谢谢!

解决方案

您不能同时使用 grid 在同一个包含小部件中。第一个将调整小部件的大小。另一个会看到变化,并调整一切以适应它自己的限制。第一个会看到这些变化,并再次调整一切以适应其限制。另一个会看到变化,等等无限。他们将陷于争夺霸权的永恒斗争中。

虽然技术上可行的是,如果您确实知道您在做什么,但对于所有意图和目的,您不能混用它们在同一个容器中。您可以将所有您想要的内容混合到您的应用程序中,但对于给定的容器(通常为框架),您只能使用一个来管理容器的直接内容。



一种非常常见的技术是将您的GUI分成几块。在你的情况下,你有一个底部的状态栏和顶部的主区域。因此,请将状态栏打包在底部,并为GUI的主要部分创建一个包装在其上方的框架。然后,其他所有内容都以主框架作为其父框架,并且在该框架内您可以使用网格或包装或任何您想要的内容。

Im new to python so please forgive my 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. 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. It seems like I can only use either pack() or grid() but not both. 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 in the same containing widget. 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. 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. Then, everything else has the main frame as its parent, and inside that frame you can use grid or pack or whatever you want.

这篇关于python pack()和grid()方法一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆