这是一个关于使用Python 3+在Windows操作系统中使用tkinter的问题。我知道如何居中Label文本(justify=CENTER)
,而且根据我搜索到的所有内容,Label文本默认为居中对齐。
可以有人查看我的下面的代码并告诉我我做错了什么吗?我已经在网上搜索了几个小时,但就是找不出我哪里做错了。
from tkinter import *
from tkinter.ttk import *
class MainApplication():
def __init__(self, master):
self.master = master
self.master.title("OOP GUI Example")
self.master.iconbitmap("Blank.ico")
label = Label(self.master, text="Test Callback", )
greet_button = Button(self.master, width=25, text="Greet", command=self.greet)
close_button = Button(self.master, width=25, text="Close", command=self.close)
Grid.columnconfigure(self.master, 0, weight=1)
Grid.rowconfigure(self.master, (0,1,2), weight=1)
label.grid(row=0, column=0, sticky="NSWE",padx=(10, 10), pady=(7.5, 0))
greet_button.grid(row=1, column=0, sticky="NSWE", padx=(10, 10), pady=(10, 1.5))
close_button.grid(row=2, column=0, sticky="NSWE", padx=(10, 10), pady=(1.5, 10))
def greet(self):
print("Greetings!")
return
def close(self):
self.master.quit()
return
root = Tk()
root.style = Style()
# ('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')
root.style.theme_use("clam")
gui = MainApplication(root)
root.mainloop()
这里的label.grid(row=0, column=0, sticky="NSWE",padx=(10, 10), pady=(7.5, 0))
语句是使文本左对齐。特别是其中的sticky="NSWE"
,但我想让标签小部件随窗口大小调整,所以我无法更改。
.pack(fill='x', ...)
或.grid(sticky='ew', ...)
水平拉伸ttk.Label
时,它并没有居中对齐。 - Nae