Tkinter Widgets彼此相邻

2024-10-01 02:34:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我的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(),这样我就可以把这些小部件放在一起,然后把另一个放在它们下面?在

提前谢谢


Tags: textselfbuttonwidthleftfillsidepack
1条回答
网友
1楼 · 发布于 2024-10-01 02:34:11

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

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是一种方法,因此应用程序主循环()

  3. CheckVar1和CheckVar2必须在类中声明
  4. camel case(大写的单词,例如'CheckVar')不是Python式的

相关问题 更多 >