【问题标题】:Tkinter Widgets next to each other and belowTkinter 小部件彼此相邻并位于下方
【发布时间】:2019-01-04 11:07:13
【问题描述】:

我将小部件放入我的 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() 的东西,以便我可以将小部件彼此相邻放置,然后将另一个放在它们下方?

提前致谢

【问题讨论】:

标签: python tkinter widget


【解决方案1】:

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

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')不是pythonic

【讨论】:

  • 这很好用,但是,我无法设置底部框架的背景,我可以通过在 tk.Checkbutton() 中包含背景颜色来设置顶部框架的颜色,但这不会为底部框架工作。有什么想法吗?
  • 创建框架的时候指定背景一样简单,所以bottomframe = Frame(popup, bg="blue")
猜你喜欢
  • 2017-02-20
  • 2021-08-04
  • 2014-05-06
  • 2020-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-13
相关资源
最近更新 更多