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

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

问题描述

我是 Python 新手,所以请原谅我的菜鸟.我试图在我的应用程序窗口底部创建一个状态栏,但似乎每次我在同一个文件中同时使用 pack() 和 grid() 方法时,主应用程序窗口都不会打开.当我注释掉 statusbar.pack(side = BOTTOM, fill = X) 的那行时,我的应用程序窗口打开得很好,但如果我把它留在里面就不会,而且如果我注释掉任何使用 grid 方法的行窗口与状态栏一起打开.似乎我只能使用 pack() 或 grid() 但不能同时使用两者.我知道我应该能够使用这两种方法.有什么建议?代码如下:

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

感谢任何帮助!谢谢!

推荐答案

您不能在具有相同母版的小部件上同时使用 packgrid.第一个将调整小部件的大小.另一个将看到更改,并调整所有内容以适应其自身的约束.第一个将看到这些更改并再次调整所有内容的大小以适应约束.另一个将看到更改,以此类推.他们将陷入永恒的至高无上的斗争中.

You cannot use both pack and grid on widgets that have the same master. 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.

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

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天全站免登陆