Tkinter GUI 不在正确的位置 [英] Tkinter GUI not in proper place

查看:34
本文介绍了Tkinter GUI 不在正确的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 Tkinter GUI,一个顶部有标签的灰色框,然后在其下方有一个下拉框,左侧有一个标签描述它,下面是另一个下拉框,左侧有一个标签.相反,我将标签放在了正确的位置,但两个下拉框和标签位于彼此的顶部,即使它们位于不同的框架中.

I am trying to create a Tkinter GUI a grey box with a label at the top, then below that a drop down box and with a label to its left describing it and below that another drop down box with a label to its left. Instead I am getting the label in the proper place but then the two drop down boxes and labels are on top of each other even though they are in different frames.

from Tkinter import *
import ttk


class review_gui():


    def __init__(self):
        self.root = Tk()

        self.EDM_GUI(1)

        self.root.mainloop()


    def EDM_GUI(self, column):
        self.EDM_frm = Frame(self.root, background = "grey")
        self.EDM_frm.pack(side = "top")

        EDM_Label_frm = Frame(self.EDM_frm).pack(side = "top")
        EDM_DD_frm = Frame(self.EDM_frm).pack(side = "bottom")

        EDM_DB_frm = Frame(EDM_DD_frm).pack(side = "top")
        EDM_Port_frm = Frame(EDM_DD_frm).pack(side = "bottom")



        EDM_lbl = Label(EDM_Label_frm, text="EDM", fg="black").pack()

        EDM_DD_label = Label(EDM_DB_frm, text="Select EDM:", fg="black")
        EDM_DD_label.pack(side="left")
        EDM_Drop_Down = ttk.Combobox(EDM_DB_frm, state="readonly")
        EDM_Drop_Down["values"] = ("One", "Two", "Three")
        EDM_Drop_Down.pack(side="right")



        EDM_P_label = Label(EDM_Port_frm, text="Select Portfolio:", fg="black")
        EDM_P_label.pack(side="left")
        Port_Drop_Down = ttk.Combobox(EDM_Port_frm, state="readonly")
        Port_Drop_Down["values"] = ("One", "Two", "Three")
        Port_Drop_Down.pack(side="right")

rg = review_gui()

推荐答案

Standart pack 几何管理器可能看起来古怪而任性,但事实并非如此.您必须了解的一件事是您的操作顺序!

Standart pack geometry manager may seem bizarre and wayward, but it's not true. One thing that you must understand is the sequence of your actions!

所以主要算法是这样的:

So main alghorithm is this:

  1. 一个(小部件或容器的)实例被创建,并与其父相关联.
  2. 实例已打包.

换句话说:想象一下你正在盖房子!在您的示例中,您开始在五金店或任何地方建造房屋,因此您的房屋并未按照您的计划建造.

In other words: imagine that you are building a house! In your example, you start building a house right in the hardware store or wherever, so your house is not built according to your plans.

试着想象每个框架都是房子的地板.里面的小部件是家具!因此,您首先要用家具规划您的地板,然后按照您的意愿打包.

Try to imagine that each frame is the floor of the house. And the widgets inside it are furniture! So first you plan your floors with furniture and then pack it in order you wish.

我稍微修改了您的 EDM_GUI 函数,它看起来像您想要的那样工作!请记住避免在一行中声明和打包,因为 pack 返回 None.

I modified little bit your EDM_GUI function and it looks like it's working like you desired! Just remember to avoid declaration and packing in one line, because pack returns None.

示例:

def EDM_GUI(self, column):
    self.EDM_frm = Frame(self.root, background="grey")
    self.EDM_frm.pack()

    EDM_Label_frm = Frame(self.EDM_frm).pack()
    EDM_DD_frm = Frame(self.EDM_frm).pack()

    EDM_DB_frm = Frame(EDM_DD_frm)
    EDM_Port_frm = Frame(EDM_DD_frm)



    EDM_lbl = Label(EDM_Label_frm, text="EDM", fg="black").pack()

    EDM_DD_label = Label(EDM_DB_frm, text="Select EDM:", fg="black")
    EDM_DD_label.pack(side="left")
    EDM_Drop_Down = ttk.Combobox(EDM_DB_frm, state="readonly")
    EDM_Drop_Down["values"] = ("One", "Two", "Three")
    EDM_Drop_Down.pack(side="right")
    EDM_DB_frm.pack(side="top")



    EDM_P_label = Label(EDM_Port_frm, text="Select Portfolio:", fg="black")
    EDM_P_label.pack(side="left")
    Port_Drop_Down = ttk.Combobox(EDM_Port_frm, state="readonly")
    Port_Drop_Down["values"] = ("One", "Two", "Three")
    Port_Drop_Down.pack(side="right")
    EDM_Port_frm.pack(side="bottom")

结果:

添加:关于 pack 管理器的保存和逻辑的问题:我不完全知道 pack 管理器在幕后如何工作,但是您可以通过向框架添加边框来测试是否出现问题!只需像这样声明您的框架:FrameVar = Frame(parent, Relief='raised', borderwidth=10).如您所见 - 外框小部件直接放置在您的 root 窗口中!

Addition: To your question about preserving and logic of pack manager: I don't exactly know how pack manager works under the hood, but you can test, if something going wrong or not by adding borders to your frames! Just declare your frames like that: FrameVar = Frame(parent, relief='raised', borderwidth=10). As you see - outframed widgets placed directly to your root window!

所以我测试了 3 个变体(来自你的问题,来自我的回答和正确的一个):

So I tested 3 variants (from your question, from my answer and correct one):

  1. 完全超出了一个(来自你的问题)

  1. 部分超出了一个(来自我的回答)

  1. 更正一项,框架中的所有项目

正确的代码:

def EDM_GUI(self, column):
    self.EDM_frm = Frame(self.root)

    EDM_Label_frm = Frame(self.EDM_frm)
    EDM_lbl = Label(EDM_Label_frm, text="EDM", fg="black").pack()
    EDM_Label_frm.pack()

    EDM_DD_frm = Frame(self.EDM_frm)

    EDM_DB_frm = Frame(EDM_DD_frm)
    EDM_DD_label = Label(EDM_DB_frm, text="Select EDM:", fg="black")
    EDM_DD_label.pack(side="left")
    EDM_Drop_Down = ttk.Combobox(EDM_DB_frm, state="readonly")
    EDM_Drop_Down["values"] = ("One", "Two", "Three")
    EDM_Drop_Down.pack(side="right")
    EDM_DB_frm.pack(side="top")

    EDM_Port_frm = Frame(EDM_DD_frm)
    EDM_P_label = Label(EDM_Port_frm, text="Select Portfolio:", fg="black")
    EDM_P_label.pack(side="left")
    Port_Drop_Down = ttk.Combobox(EDM_Port_frm, state="readonly")
    Port_Drop_Down["values"] = ("One", "Two", "Three")
    Port_Drop_Down.pack(side="right")
    EDM_Port_frm.pack(side="bottom")

    EDM_DD_frm.pack()
    self.EDM_frm.pack()

P.S.仅当您喜欢全局命名空间污染时才使用通配符导入.

这篇关于Tkinter GUI 不在正确的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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