首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在tkinker中使用.pack正确定位widgets?

如何在tkinker中使用.pack正确定位widgets?
EN

Stack Overflow用户
提问于 2017-03-16 07:59:11
回答 1查看 78关注 0票数 0

我正在为我的大学班级做一个项目,试图为一些东西做一个tkinker GUI,但在它的正确定位上遇到了困难。

下面是我的代码:

代码语言:javascript
复制
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()

问题是,我希望顶部按钮在文本和列表框小部件的下方,列表框标记为健康的下方,我尝试将它们打包到顶部和底部,但都不起作用,我在google或这里找不到解决方案,有人知道我会怎么做吗?

另外,如果缩进不好,我很抱歉,第一次在这里,我不确定如何粘贴代码而不弄乱缩进。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-16 22:06:14

粗略的想法见下文:

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42822963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档