Tkinter窗口小部件彼此相邻并位于下方

时间:2019-01-04 11:07:13

标签: python tkinter widget

我正在将小部件放入GUI的弹出窗口。我将复选框放在弹出窗口的顶部,我希望标签,条目和按钮位于它们下方的行中。无论我尝试使用哪种标签,条目和按钮都始终位于复选框旁边。我还没有找到使用pack()的解决方案。我已经尝试过anchor=,但这也没有满足我的要求。

这是我的代码:

import tkinter as tk
from tkinter import *
CheckVar1 = IntVar()
CheckVar2 = IntVar()


    class PopUp(tk.Tk):
        def __init__(self):
            tk.Tk.__init__(self)
            popup = tk.Toplevel(self, background='gray15')
            popup.wm_title("EMAIL")
            self.withdraw()
            popup.tkraise(self)
            self.c1 = tk.Checkbutton(popup, text="Current", variable=CheckVar1, onvalue =1, offvalue = 0, height=2, width=15)
            self.c1.pack(side="left", fill="x")
            self.c2 = tk.Checkbutton(popup, text="-1", variable=CheckVar2, onvalue =1, offvalue = 0, height=2, width=15)
            self.c2.pack(side="left", fill="x")
            label = tk.Label(popup, text="Please Enter Email Address", background='gray15', foreground='snow')
            label.pack(side="left", fill="x", pady=10, padx=10)
            self.entry = tk.Entry(popup, bd=5, width=35, background='gray30', foreground='snow')
            self.entry.pack(side="left", fill="x")
            self.button = tk.Button(popup, text="OK", command=self.on_button, background='gray15', foreground='snow')
            self.button.pack(side="left", padx=10)

        def on_button(self):
            address = self.entry.get() 
            print(address)
            time.sleep(10)
            self.destroy()


    app = PopUp()
    app.mainloop

是否可以在pack()中放入一些内容,以便可以将小部件彼此相邻,然后将其他小部件置于其下方?

预先感谢

1 个答案:

答案 0 :(得分:2)

使用网格几何管理器将窗口分为两个框架,然后将小部件打包到框架中。

from tkinter import *
import time


class PopUp(Tk):
    def __init__(self):
        Tk.__init__(self)

        CheckVar1 = IntVar()
        CheckVar2 = IntVar()
        popup = Toplevel(self, background='gray15')
        popup.wm_title("EMAIL")
        self.withdraw()
        popup.tkraise(self)

        topframe = Frame(popup)
        topframe.grid(column=0, row=0)

        bottomframe = Frame(popup)
        bottomframe.grid(column=0, row=1)

        self.c1 = Checkbutton(topframe, text="Current", variable=CheckVar1, onvalue=1, offvalue=0, height=2, width=15)
        self.c1.pack(side="left", fill="x")
        self.c2 = Checkbutton(topframe, text="-1", variable=CheckVar2, onvalue=1, offvalue=0, height=2, width=15)

        self.c2.pack(side="left", fill="x")
        label = Label(bottomframe, text="Please Enter Email Address", background='gray15', foreground='snow')
        label.pack(side="left", fill="x", pady=10, padx=10)
        self.entry = Entry(bottomframe, bd=5, width=35, background='gray30', foreground='snow')
        self.entry.pack(side="left", fill="x")
        self.button = Button(bottomframe, text="OK", command=self.on_button, background='gray15', foreground='snow')
        self.button.pack(side="left", padx=10)

    def on_button(self):
        address = self.entry.get()
        print(address)
        time.sleep(10)
        self.destroy()


app = PopUp()
app.mainloop()

一些注意事项:

  1. 无需两次导入Tkinter

  2. Mainloop是一个方法,所以它是app.mainloop()

  3. CheckVar1和CheckVar2必须在您的类中声明
  4. 驼峰式大小写字母(例如“ CheckVar”大写)不是python