我有如下代码。我想确保textArea1将在textAreaLabel1旁边,但是现在它离左边很远,因为第一个对象位于posistion行= 0,列= 0且非常长。 我该如何使其紧挨着呢?
import tkinter as tk
class MainFrame(tk.Tk):
""" This is main class for managing different views(windows) """
#-------------------------------------------------------------------------------------
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# init main frame
container = tk.Frame(self, width=500, height=300)
# container.pack(side="top", fill="both", expand = True)
# stores starting options
self.checkBoxDict = dict()
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
# add start info
entryInfo = tk.Label(text = "Choose your settings, one window running different algorithm will be open for every check box checked")
entryInfo.grid(row = 0, column = 0)
# add rest of controlers
self.addControlers()
#-------------------------------------------------------------------------------------
def addControlers(self):
"""sets rest of gui"""
info = tk.Label(text = "Choose what algorithms(crossover) you want to compare")
info.grid(row = 1, column = 0, sticky="W")
textAreaLabel1 = tk.Label(text = "How big population ? (best 100-500)")
textAreaLabel1.grid(row = 3, column = 0, sticky="w")
textArea1 = tk.Entry()
textArea1.grid(row = 3, column = 1,sticky="w" )
textAreaLabel2 = tk.Label(text = "How many points ? (best 20-50)")
textAreaLabel2.grid(row = 4, column = 0, sticky="w")
textArea2 = tk.Entry()
textArea2.grid(row = 4, column = 1, sticky="W")
self.checkBoxDict["ramdom"] = tk.BooleanVar()
checkButton1 = tk.Checkbutton( text="Pure randomness approach", variable=self.checkBoxDict["ramdom"])
checkButton1.grid(row = 5, column = 0, sticky="W")
self.checkBoxDict["pmx"] = tk.BooleanVar()
checkButton2 = tk.Checkbutton( text="PMX crossover", variable=self.checkBoxDict["pmx"])
checkButton2.grid(row = 6, column = 0, sticky="W")
self.checkBoxDict["mutation"] = tk.BooleanVar()
checkButton3 = tk.Checkbutton( text="Only mutation no crossover", variable=self.checkBoxDict["mutation"] )
checkButton3.grid(row = 7, column = 0, sticky="W")
#-------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------
app = MainFrame()
app.mainloop()
更新 如我所愿,它开始为我工作
...
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(2, weight=1)
# add start info
entryInfo = tk.Label(text = "Choose your settings, one window running different algorithm will be open for every check box checked")
entryInfo.grid(row = 0, column = 0, columnspan=10)
# add rest of controlers
self.addControlers()
#-------------------------------------------------------------------------------------
def addControlers(self):
"""sets rest of gui"""
info = tk.Label(text = "Choose what algorithms(crossover) you want to compare")
info.grid(row = 1, column = 0, sticky="w", columnspan=10)
textAreaLabel1 = tk.Label(text = "How big population ? (best 100-500)")
textAreaLabel1.grid(row = 3, column = 0, sticky="w")
textArea1 = tk.Entry()
textArea1.grid(row = 3, column = 1,sticky="w")
textAreaLabel2 = tk.Label(text = "How many points ? (best 20-50)")
textAreaLabel2.grid(row = 4, column = 0, sticky="w")
textArea2 = tk.Entry()
textArea2.grid(row = 4, column = 1, sticky="W")
self.checkBoxDict["ramdom"] = tk.BooleanVar()
checkButton1 = tk.Checkbutton( text="Pure randomness approach", variable=self.checkBoxDict["ramdom"])
checkButton1.grid(row = 5, column = 0, sticky="W")
self.checkBoxDict["pmx"] = tk.BooleanVar()
checkButton2 = tk.Checkbutton( text="PMX crossover", variable=self.checkBoxDict["pmx"])
checkButton2.grid(row = 6, column = 0, sticky="W")
self.checkBoxDict["mutation"] = tk.BooleanVar()
checkButton3 = tk.Checkbutton( text="Only mutation no crossover", variable=self.checkBoxDict["mutation"] )
checkButton3.grid(row = 7, column = 0, sticky="W")
startButton = tk.Button(text = "Start", bd = 3, bg = "#20aa20", command = lambda:self.start())
startButton.grid(row = 8, sticky = "nswe", columnspan=10)
#-------------------------------------------------------------------------------------
答案 0 :(得分:1)
最简单的解决方案是将用户界面视为由三列而不是两列组成,第三列是长标签所需的额外空间。
要做到这一点,只需使这两个长标签的columnspan
为3,然后对第二或第三列赋予权重,以使其根据长标签的需要进行扩展。
这显示了如何使文本跨多列:
entryInfo.grid(row = 0, column = 0, stickky="W", columnspan=3)
...
info.grid(row = 1, column = 0, sticky="W", columnspan=3)
这显示给第三列赋予权重:
class MainFrame(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.grid_columnconfigure(2, weight=1)
...