我正在为大学课堂上的一个项目工作,试图为某些东西制作一个tkinker GUI,但我很难在其中获得定位。
这是我的代码:
from tkinter import *
from sys import exit
def button_func():
print("Test")
class TestClient(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.pack()
w = Listbox(width=20,height=24)
w.insert(1,"WoW")
w.pack(side=LEFT)
w = Text(width=60)
w.pack(side=LEFT)
w = Listbox(width=20,height=24)
w.insert(1,"Hi")
w.pack(side=RIGHT)
w = Button(self, text="Start", width=10,padx=10,pady=10, command=button_func)
w.pack(side=LEFT)
w = Button(self, text="Change Room", width=10,padx=10,pady=10, command=button_func)
w.pack(side=LEFT)
w = Button(self, text="Change Room", width=10,padx=10,pady=10, command=button_func)
w.pack(side=LEFT)
w = Button(self, text="FIGHT", width=10,padx=10,pady=10, command=button_func)
w.pack(side=LEFT)
w = Button(self, text="PvP FIGHT", width=10,padx=10,pady=10, command=button_func)
w.pack(side=LEFT)
w = Button(self, text="Loot", width=10,padx=10,pady=10, command=button_func)
w.pack(side=LEFT)
w = Button(self, text="Leave", width=10,padx=10,pady=10, command=button_func)
w.pack(side=LEFT)
stats = Listbox(width= 20)
stats.insert(1,"health:")
stats.pack(side=BOTTOM)
root = Tk()
root.title = "Test program"
tw = TestClient(root)
root.mainloop()
问题是我希望顶部按钮位于文本和列表框小部件下方,而标记为health的列表框低于该值,我尝试将它们打包到顶部和底部,但是既不能正常工作也无法找到谷歌或这里的洗礼,有谁知道我会怎么做?
P.S。对不起,如果缩进不好,第一次在这里,我不知道如何粘贴代码而不会弄乱缩进。
答案 0 :(得分:0)
请参阅下面的粗略想法:
from tkinter import *
from sys import exit
def button_func():
print("Test")
class TestClient(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.pack()
for n in range(3):
self.grid_rowconfigure(n, weight=1)
for n in range(8):
self.grid_columnconfigure(n, weight=1)
lb1 = Listbox(self, width=20,height=24)
lb1.insert(1,"WoW")
lb1.grid(row=0, column=0, columnspan=2, sticky='news')
t1 = Text(self, width=60)
t1.grid(row=0, column=3, columnspan=3)
lb2 = Listbox(self, width=20,height=24)
lb2.insert(1,"Hi")
lb2.grid(row=0, column=6, columnspan=2, sticky='news')
b1 = Button(self, text="Start", width=10,padx=10,pady=10, command=button_func)
b1.grid(row=1, column=0)
b2 = Button(self, text="Change Room", width=10,padx=10,pady=10, command=button_func)
b2.grid(row=1, column=1)
b3 = Button(self, text="Change Room", width=10,padx=10,pady=10, command=button_func)
b3.grid(row=1, column=3)
b3 = Button(self, text="FIGHT", width=10,padx=10,pady=10, command=button_func)
b3.grid(row=1, column=4)
b4 = Button(self, text="PvP FIGHT", width=10,padx=10,pady=10, command=button_func)
b4.grid(row=1, column=5)
b5 = Button(self, text="Loot", width=10,padx=10,pady=10, command=button_func)
b5.grid(row=1, column=6)
b6 = Button(self, text="Leave", width=10,padx=10,pady=10, command=button_func)
b6.grid(row=1, column=7)
stats = Listbox(self, width= 20)
stats.insert(1,"health:")
stats.grid(row=2, column=0, columnspan=8, sticky='news')
root = Tk()
root.title = "Test program"
tw = TestClient(root)
root.mainloop()