首页 > 解决方案 > Tkinter 按钮分散得很远,如何将它们放在一起?

问题描述

使用 Tkinter 时,我在屏幕顶部制作几个按钮时遇到问题,说“常见问题解答”“用户手册”等,它们相距太远,我尝试更改行和列,我认为这是因为填充但事实并非如此,我什至将它们放入按钮框架中,以为可以修复它但无济于事。

from tkinter import *
from tkinter import ttk
import methods as fn
from subprocess import call
#functions but most of other funcs are in a methods.py in the same folder

def close_root(): #kill func
    root.destroy()
    exit()

class Main: #Main class
    root = Tk() #init for main window
    root.title("TKL")
    root.configure(background = "#f16161")

    #row 0
    #photo
    logo = PhotoImage(file = "images/TK_logo.png")
    Label(root, image=logo, bg="#f16161").grid(row = 0, column = 0, pady=5, sticky = W)

    #here is where the issue arises SO friends
    buttonframe = Frame(root).grid(row = 0, column = 1) #toolbar on top of the window
    Button(buttonframe, text = "FAQ", command = fn.documentTKL, font = "none 12", bg="#df9999", width = 5, height = 1).grid(row = 0, column = 1, pady = 5, sticky = NW)
    Button(buttonframe, text = "Manual", command = fn.documentTKL, font = "none 12", bg="#df9999", width = 5, height = 1).grid(row = 0, column = 1, pady = 5, sticky = NW)
    Button(buttonframe, text = "add something here later", command = fn.documentTKL, font = "none 12", bg="#df9999", width = 5, height = 1).grid(row = 0, column = 1, pady = 5, sticky = NW)

    #row 1
    Label(root, text = "\nTransKazLit", bg = "#f16161", fg = "white", font = "none 12 bold").grid(row = 1, column = 0, padx = 10, pady = 5, sticky = W)
    Button(root, text = "Document to Text", command = fn.documentTKL, font = "none 12", bg="#df9999", width = 20, height = 5).grid(row = 1, column = 1, padx = 10, pady = 5, sticky = E)
    Button(root, text = "Real Time Transliteration", command = fn.realtimeTKL, font = "none 12", bg="#df9999", width = 20, height = 5).grid(row = 1, column = 2, padx = 10, pady = 5, sticky = E)

    #row 2
    Button(root, text = "Settings", font = "none 12", bg="#df9999", width = 20, height = 5).grid(row = 2, column = 2, padx = 10, pady = 5, sticky = E)

    #exit
    Button(root, text = "EXIT", font = "none 10", bg="#FF4C4C", width = 20, height = 5, command = close_root).grid(row = 10, column = 0, padx = 10, pady = 5, sticky = W)

    root.resizable(1, 1)
    root.iconbitmap('images/TK_logo.ico')
    root.mainloop()

这是代码,如果您想对其进行测试并了解我的具体意思。它的那个特殊的三重按钮似乎不能正常工作,其他一切正常。

标签: pythonpython-3.xwindowstkinter

解决方案


您的问题中的代码中有很多无关紧要的东西,因此它不是一个最小的、可重现的示例,而且由于您没有遵循 PEP 8 - Python 代码样式指南指南,因此阅读起来有些困难

尽管如此,我认为至少部分问题是因为这条线:

buttonframe = Frame(root).grid(row = 0, column = 1) #toolbar on top of the window

分配给Nonebuttonframe因为这是grid()方法总是返回的。然后,在此工具栏中parent创建三个时,您可以使用该虚假值作为参数。Buttons

另一个问题来自于不理解这个新框架有自己的嵌套网格,该网格独立于外部网格(因此用于其中项目的任何行和列值都应该与其位置相关)。

这是解决大多数这些问题的代码的修改版本:

from tkinter import *
from tkinter import ttk
#import methods as fn
from subprocess import call
# Functions but most of other funcs are in a methods.py in the same folder.

class Main:  # Main class.
    def __init__(self):
        root = self.root = Tk()  # Init for main window.
        root.title("TKL")
        root.configure(background="#f16161")

        # Row 0
        # Photo
        logo = PhotoImage(file="images/TK_logo.png")
        Label(root, image=logo, bg="#f16161").grid(row=0, column=0, pady=5, sticky=W)
        Label(root, text='Logo Here', bg="#f16161").grid(row=0, column=0, pady=5, sticky=W)

        btnframe = Frame(root, bg="#f16161")
        btnframe.grid(row=0, column=1) # Toolbar on top of the window.

        faq_btn = Button(btnframe, text="FAQ", command=fn.documentTKL, font="none 12",
                         bg="#df9999", width=5, height=1)
        faq_btn.grid(row=0, column=0, pady=5, sticky=NW)
        man_btn = Button(btnframe, text="Manual", command=fn.documentTKL, font="none 12",
                         bg="#df9999", width=5, height=1)
        man_btn.grid(row=0, column=1, pady=5, sticky=NW)
        shl_btn = Button(btnframe, text="add something here later",
                         command=fn.documentTKL, font="none 12", bg="#df9999",
                         width=5, height=1)
        shl_btn.grid(row=0, column=2, pady=5, sticky=NW)

        # Row 1
        lbl = Label(root, text="TransKazLit", bg="#f16161", fg="white",
                    font="none 12 bold")
        lbl.grid(row=1, column=0, padx=10, pady=5, sticky=W)
        btn = Button(root, text="Document to Text", command=fn.documentTKL,
                     font="none 12", bg="#df9999", width=20, height=5)
        btn.grid(row=1, column=1, padx=10, pady=5, sticky=E)
        btn = Button(root, text="Real Time Transliteration", command=fn.realtimeTKL,
                     font="none 12", bg="#df9999", width=20, height=5)
        btn.grid(row=1, column=2, padx=10, pady=5, sticky=E)

        # Row 2
        btn = Button(root, text="Settings", font="none 12", bg="#df9999", width=20,
                     height=5)
        btn.grid(row=2, column=2, padx=10, pady=5, sticky=E)

        # Exit
        btn = Button(root, text="EXIT", font="none 10", bg="#FF4C4C", width=20, height=5,
                     command=self.close_root)
        btn.grid(row=10, column=0, padx=10, pady=5, sticky=W)

        root.resizable(1, 1)
        root.iconbitmap('images/TK_logo.ico')
        root.mainloop()

    def close_root(self):  # Kill func.
        self.root.destroy()
        exit()


Main()


推荐阅读