简体   繁体   English

Python Tkinter窗口/框架调整问题对齐问题2.7

[英]Python Tkinter Window/Frame adjustment issues aligning issues 2.7

I am doing a project at home just trying to create a window with 3(technically 4) frames. 我在家里做一个项目,只是想创建一个具有3(技术上为4)帧的窗口。 I have an upper frame that has 2 frames inside of it(I want a left and right Frame) then I have a lower Frame that covers everything else. 我有一个上部框架,内部有2个框架(我想要一个左右框架),然后有一个下部框架覆盖了其他所有东西。 That lower frame will eventually have an external process in it, but for now an image that will not take up the full space. 较低的框架最终将具有外部处理,但目前为止,该图像将不会占据整个空间。 The upper space will not split evenly, EVEN THOUGH I split the height and width evenly at one point. 上部空间不会平均分配,即使我在某一点平均分配了高度和宽度。 I will send my code and show an image below. 我将发送代码并在下面显示图片。

def createFrames(self):
    #Main Upper Frame
    topFrame = Frame(height=120, width=800, bd=1, relief=SUNKEN)
    topFrame.pack(side=TOP)

    #Left Frame in Main Upper Frame
    topFrameLeft = Frame(topFrame, height=120, width=400)
    topFrameLeft.pack(side=LEFT)

    #Right Frame in Main Upper Frame
    topFrameRight = Frame(topFrame, height=120, width=400)
    topFrameRight.pack(side=RIGHT)

    #Frame for GPS, Lower
    centerFrame = Frame(width=800, height=400, bg="",
                        colormap="new",bd=3, relief=GROOVE)
    centerFrame.pack(side=BOTTOM, fill=BOTH, expand=True)

    #photo stuff
    photo = PhotoImage(file="GPS_Imitation.gif")
    #scale_w = 3
    #scale_h = 400/200
    #photo = photo.zoom(scale_w, scale_h)
    #photo = photo.subsample(1)
    Image_Label = Label(centerFrame, image=photo)
    Image_Label.photo = photo
    Image_Label.pack(fill=BOTH, expand=True)

    #Label for Left Frame
    Left_Label = Label(topFrameLeft, width=56, text="Audio", bg="gray",
                       fg="blue")
    Left_Label.pack()

    #Label for Right Frame
    Right_Label = Label(topFrameRight, width=60,
                        text="Phone/Notification",
                        bg="Green", fg="Black")
    Right_Label.pack()

橱窗图片

I posted the function. 我发布了功能。 I had to do some weird stuff with the code to get it to do this much. 我必须用代码做一些奇怪的事情,才能做到这一点。 but the picture won't expand, it just grays out under and above the picture. 但图片不会扩展,只是在图片下方和上方显示为灰色。 and I had to modify the topleft and topright labels in width and height at random. 而且我不得不随意修改宽度和高度的topleft和topright标签。 Any help would be appreciated it. 任何帮助将不胜感激。 This is written in Python using Tkinter! 这是使用Tkinter用Python编写的!

It's hard to give a definitive answer because GUI layout really depends a lot on the specifics. 很难给出确切的答案,因为GUI布局实际上很大程度上取决于具体细节。 What goes in the frames? 框架中发生了什么? How do you want them to behave when you resize? 调整大小时,您希望它们如何表现? What happens if the window is too small? 如果窗口太小会怎样?

If I were doing this I would probably get rid of the internal frames and just put everything in a grid since it seems that you have two columns and two or three rows. 如果这样做,我可能会摆脱内部框架,而将所有内容放入网格中,因为看来您有两列和两三行。 Though, that decision really depends on what else is going to be put in various rows and columns. 但是,该决定实际上取决于要在各行和各列中加上什么。

There's nothing wrong with using pack and extra helper frames (this is often my first choice!), grid is arguably the best tool for the job if you want the width of two columns or more columns to be identical. 使用pack和额外的辅助框架没有什么错(这通常是我的第一选择!),如果您希望两列或更多列的宽度相同,则grid可以说是完成此工作的最佳工具。

Grid allows you to configure columns to be in a uniform group . 网格允许您将列配置为统一组 Every column with the same value for the uniform attribute will be the same size. 同为相同值的每一列uniform属性将是相同的大小。 So, for example, to make sure that topFrameLeft and topFrameRight are exactly the same you can put them in a uniform group inside of topFrame . 因此,例如,要确保topFrameLefttopFrameRight完全相同,可以将它们放在topFrame内的统一组中。

Start by using grid to place the widgets inside of TopFrame : 首先使用grid将小部件放置在TopFrame

topFrameLeft.grid(row=0, column=0, sticky="nsew")
topFrameRight.grid(row=0, column=1, sticky="nsew")

Next, configure the uniform columns. 接下来,配置统一列。 Note: it's a best practice to always give at least one row and one column a weight, even if you use only one row or one column. 注意:最好的做法是始终给至少一行和一列赋予权重,即使您仅使用一行或一列也是如此。

topFrame.grid_rowconfigure(0, weight=1)
topFrame.grid_columnconfigure(0, uniform="half", weight=1)
topFrame.grid_columnconfigure(1, uniform="half", weight=1)

Note: You can continue to use pack for all the other widgets. 注意:您可以继续将pack用于所有其他小部件。 You can freely mix and match pack , place and grid within an application as long as you don't mix them with widgets that share the same parent. 您可以在应用程序中自由混合和匹配packplacegrid ,只要您不将它们与共享同一父级的小部件混合即可。

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

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