【问题标题】:Tkinter Buttons are spread way to far apart, how to put them next to each other?Tkinter 按钮分散得很远,如何将它们放在一起?
【发布时间】:2020-05-19 11:01:35
【问题描述】:

使用 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()

如果你想测试一下,看看我的具体意思,这里是代码。那个特殊的三重按钮似乎不能正常工作,其他一切正常。

【问题讨论】:

  • 您是否考虑过使用place 而不是grid?您还可以在同一列中将按钮设置为NWNNE。我真的不知道你的图像大小,所以我只能复制你的问题。
  • 由于问题是关于布局的,如果您的示例不依赖于外部模块和图像会有所帮助。
  • @Axe319 place() 不是这里的解决方案。 OP 只需要一个好的 grid()pack() 示例即可使用。 place() 应保留用于非常特殊的需求,在大多数其他情况下,grid()pack() 是布局的正确选择。

标签: python python-3.x windows tkinter


【解决方案1】:

解决方案是将按钮放在框架中。你试图这样做,但你做错了。您将buttonframe 设置为None,导致所有按钮都添加到根窗口。

此外,使用pack 通常比使用grid 更容易对齐单行或单列中的内容,因此我建议使用pack 将按钮放在按钮框架中。

将您的代码更改为如下所示。注意:为清楚起见,我删除了与布局问题无关的代码。

buttonframe = Frame(root)
buttonframe.grid(row = 0, column = 1) 

faq_button = Button(buttonframe, text = "FAQ", ...)
manual_button = Button(buttonframe, text = "Manual", ...)
something_button = Button(buttonframe, text = "add something here later", ...)

faq_button.pack(side="left")
manual_button.pack(side="left")
something_button.pack(side="left")

根据我的经验,当所有布局代码都在一个单独的块中时,布局问题更容易解决,而不是在一行中塞满尽可能多的代码。

【讨论】:

    【解决方案2】:

    您的问题中的代码中有很多无关的东西,所以它不是minimal, reproducible example加上它有点难以阅读,因为您没有遵循PEP 8 - Style Guide for Python Code 准则。

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

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

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

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

    以下是解决大部分问题的代码的修改版本:

    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()
    
    

    【讨论】:

    • Runley:不知道我是怎么错过的,但是在再看一下我发布的答案(和你的问题)之后,我突然意识到Main 是一个 class,而不是函数,定义……并且在以“有效”的方式使用它时,就类的应有方式而言,它基本上是错误的。鉴于此,我修改了我的答案,以便正确定义和使用该类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 2018-05-29
    相关资源
    最近更新 更多