【发布时间】: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?您还可以在同一列中将按钮设置为NW、N和NE。我真的不知道你的图像大小,所以我只能复制你的问题。 -
由于问题是关于布局的,如果您的示例不依赖于外部模块和图像会有所帮助。
-
@Axe319
place()不是这里的解决方案。 OP 只需要一个好的grid()或pack()示例即可使用。place()应保留用于非常特殊的需求,在大多数其他情况下,grid()和pack()是布局的正确选择。
标签: python python-3.x windows tkinter