1
我一直在使用Tkinter和Python 2.7一起放置一個菜單。它在功能上起作用,但佈局非常混亂。我使用.grid()方法來組織窗口小部件,但它們互相干擾,所以我的列表框窗口部件比按鈕高,所以它們間隔開等。我能做些什麼來解決這個問題? 我得到的結果如下所示:Tkinter小部件佈局是否錯誤?
用什麼我想在這裏實現引出:
這是我的代碼:
import Tkinter as tk
root = tk.Tk()
root.title("Test")
command = []
class button():
def __init__(self, root, text_in, x, y, command):
self.text = text_in
self.pressed = False
self.button = tk.Button(root, text=text_in, width = 15, height = 5, command = lambda: add_to_command(self.text))
self.button.grid(row=x, column = y)
def add_to_command(word):
command.append(word)
command.append(' ')
def print_command(command, command_line):
if command_line.get():
command = command_line.get()
else:
command = ''.join(command)
print command
button1 = button(root, "1", 0, 1, command)
button2 = button(root, "2", 0, 2, command)
button3 = button(root, "3", 1, 1, command)
button4 = button(root, "4", 1, 2, command)
button5 = button(root, "5", 2, 1, command)
button6 = button(root, "6", 2, 2, command)
button7 = button(root, "7", 3, 1, command)
button8 = button(root, "8", 3, 2, command)
command_line = tk.Entry(root, width = 100, takefocus=1)
command_line.grid(row = 0,column = 3, padx = 10)
go_button = tk.Button(root, text="Enter", width = 15, height = 5, command = lambda: print_command(command, command_line))
go_button.grid(row=0, column=5)
list_box = tk.Listbox(root)
list_box.grid(row = 1, column = 3)
list_box.insert(0, "ENTRY 1")
list_box.insert(1, "ENTRY 2")
root.mainloop()
我會建議一個左邊的框架按鈕,右邊的框架爲條目窗口小部件和列表框。然後您可以獨立於另一個佈局每個部分。 –