python - 如何使用 .grid 使用 tkinter 格式化我的 GUI?

标签 python python-3.x tkinter

我正在尝试使用 .grid 来格式化我的 GUI,但它没有做任何事情。下面是我的代码和我想要的布局!

enter image description here 这是我正在使用的代码。我对 Python 还很陌生...

我不确定除了 .grid 之外还有其他格式化方法,所以任何其他选项也都很棒!

from tkinter import *

class PayrollSummary:
    def __init__(pay):
        window = Tk()
        window.title("Employee Payroll")

        #Add Frame 1
        frame1 = Frame(window)
        frame1.pack()

        #Add ReadFile Button
        btReadFile = Button(frame1, text = "Read File")
        btReadFile.pack()

        #Add ShowPayroll Button
        btShowPayroll = Button(frame1, text = "Show Payroll")
        btShowPayroll.pack()

        #Add FindEmployee by Name Button
        btFindEmployee = Button(frame1, text = "Find Employee by Name")
        btFindEmployee.pack()

        #Add Highest Radio Button
        rbHigh = Radiobutton(frame1, text = "Highest")
        rbHigh.pack()

        #Add Lowest Radio Button
        rbLow = Radiobutton(frame1, text ="Lowest")
        rbLow.pack()

        #Add FindEmployee by Amount Button
        btFindEmployee_A = Button(frame1, text = "Find Employee by Amount")
        btFindEmployee_A.pack()

        #Add WriteOutput Button
        btOutput = Button(frame1, text = "Write Output to File")
        btOutput.pack()

        #Add Cancel Button
        btCancel = Button(frame1, text = "Cancel")
        btCancel.pack()



        btReadFile.grid(row = 1, column = 2)
        btShowPayroll.grid(row = 2, column = 2)
        btFindEmployee.grid(row = 2, column = 4)
        rbHigh.grid(row = 3, column = 2)
        rbLow.grid(row = 3, column = 4)
        btFindEmployee_A.grid(row = 3, column = 6)
        btOutput.grid(row = 4, column = 2)
        btCancel.grid(row = 4, column = 4)



        window.mainloop()

PayrollSummary()

最佳答案

pack()grid()place() 是将小部件放入窗口(或其他小部件)中的三种方法。
如果您将 grid() 与某些小部件一起使用,则不要使用 pack()place()

effbot.org 上的文档:grid , pack , place

我删除了除 frame.pack() 之外的所有 .pack() 并得到了几乎您所期望的结果。

enter image description here

现在小部件需要通过 sticky='w' 向左对齐('west')

添加第二个框架后我得到了

enter image description here

代码:

from tkinter import *

class PayrollSummary:
    def __init__(pay):
        window = Tk()
        window.title("Employee Payroll")

        #Add Frame 1
        frame1 = Frame(window)
        frame1.pack()

        #Add ReadFile Button
        btReadFile = Button(frame1, text = "Read File")

        #Add ShowPayroll Button
        btShowPayroll = Button(frame1, text = "Show Payroll")

        #Add FindEmployee by Name Button
        btFindEmployee = Button(frame1, text = "Find Employee by Name")

        #Add Highest Radio Button
        rbHigh = Radiobutton(frame1, text = "Highest")

        #Add Lowest Radio Button
        rbLow = Radiobutton(frame1, text ="Lowest")

        #Add FindEmployee by Amount Button
        btFindEmployee_A = Button(frame1, text = "Find Employee by Amount")

        #Add WriteOutput Button
        btOutput = Button(frame1, text = "Write Output to File")

        #Add Cancel Button
        btCancel = Button(frame1, text = "Cancel")

        btReadFile.grid(row = 1, column = 2, sticky='w')
        btShowPayroll.grid(row = 2, column = 2, sticky='w')
        btFindEmployee.grid(row = 2, column = 4, sticky='w')
        rbHigh.grid(row = 3, column = 2, sticky='w')
        rbLow.grid(row = 3, column = 4, sticky='w')
        btFindEmployee_A.grid(row = 3, column = 6, sticky='w')
        btOutput.grid(row = 4, column = 2, sticky='w')
        btCancel.grid(row = 4, column = 4, sticky='w')

        #Add Frame 2
        frame2 = Frame(window, bg='red')
        frame2.pack(fill='both') # try without `fill`

        label2 = Label(frame2, text='Label in bottom Frame', bg='green')
        label2.pack()

        window.mainloop()

PayrollSummary()

关于python - 如何使用 .grid 使用 tkinter 格式化我的 GUI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55679948/

相关文章:

python - 根据值对 DataFrame 的列进行排序

python - 安装没有依赖项的python包

python - 如何使用 python-docx 添加带有索引的数据框

python - 是否可以在机器上没有 Microsoft Office 的情况下运行 win32com 脚本

python - .exe 图标不改变 [py2exe]

python - 即时创建一个动态的 2D numpy 数组

python - 使用 Python 处理 Excel 表格

python - 在 tkinter 上的同一窗口中绘制图形和表格

python - 从 Tkinter 标签中删除 {}

Python Tkinter :When string with space inserted into Listbox, 括号出现