找到你要的答案

Q:Packing stuff in a line - Python tkinter

Q:包装的东西在一个行的Python Tkinter

I have 5 labels as follows:

Pack them in order, making sure that the one in the middle is configured to expand: import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.one = tk.Label(text="1", borderwidth=1, relief="raised")
        self.two = tk.Label(text="2", borderwidth=1, relief="raised")
        self.three = tk.Label(text="3", borderwidth=1, relief="raised")
        self.four = tk.Label(text="4", borderwidth=1, relief="raised")
        self.five = tk.Label(text="5", borderwidth=1, relief="raised")

        self.one.pack(side="left")
        self.two.pack(side="left")
        self.three.pack(side="left", fill="x", expand=True)
        self.four.pack(side="left")
        self.five.pack(side="left")

And want them to display as follows:

1 2 3 4 5

Which means that they must be in a line and self.reStarter must be centered between 2 and 5 (i.e. self.mineCount and self.flagTrackerI).

With the current code I get 1 and 2 beside each other in the top left corner and 3 a bit away (not probably in the center) as it pushes 4 5 in the second line as shown below

1 2           3
                        4 5

Can somebody help me get this working or suggest where I am going wrong. Thanks in advance.

我有5个标签如下:

将它们打包,确保中间的一个被配置为扩展: import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.one = tk.Label(text="1", borderwidth=1, relief="raised")
        self.two = tk.Label(text="2", borderwidth=1, relief="raised")
        self.three = tk.Label(text="3", borderwidth=1, relief="raised")
        self.four = tk.Label(text="4", borderwidth=1, relief="raised")
        self.five = tk.Label(text="5", borderwidth=1, relief="raised")

        self.one.pack(side="left")
        self.two.pack(side="left")
        self.three.pack(side="left", fill="x", expand=True)
        self.four.pack(side="left")
        self.five.pack(side="left")

并希望它们显示如下:

2 3 4 5

这意味着他们必须在线路和self.restarter必须集中2和5之间(即self.minecount和自我。flagtrackeri)。

与目前的代码,我得到1和2旁边对方在左上角和3有点远(不可能在中心),因为它推动4在第二行如下所示

1 2           3
                        4 5

有人能帮我得到这个工作或建议我去哪里出错。先谢谢了。

answer1: 回答1:

Pack them in order, making sure that the one in the middle is configured to expand:

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.one = tk.Label(text="1", borderwidth=1, relief="raised")
        self.two = tk.Label(text="2", borderwidth=1, relief="raised")
        self.three = tk.Label(text="3", borderwidth=1, relief="raised")
        self.four = tk.Label(text="4", borderwidth=1, relief="raised")
        self.five = tk.Label(text="5", borderwidth=1, relief="raised")

        self.one.pack(side="left")
        self.two.pack(side="left")
        self.three.pack(side="left", fill="x", expand=True)
        self.four.pack(side="left")
        self.five.pack(side="left")

if __name__ == "__main__":
    root = tk.Tk()
    root.geometry("200x100")
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

将它们打包,确保中间的一个被配置为扩展:

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.one = tk.Label(text="1", borderwidth=1, relief="raised")
        self.two = tk.Label(text="2", borderwidth=1, relief="raised")
        self.three = tk.Label(text="3", borderwidth=1, relief="raised")
        self.four = tk.Label(text="4", borderwidth=1, relief="raised")
        self.five = tk.Label(text="5", borderwidth=1, relief="raised")

        self.one.pack(side="left")
        self.two.pack(side="left")
        self.three.pack(side="left", fill="x", expand=True)
        self.four.pack(side="left")
        self.five.pack(side="left")

if __name__ == "__main__":
    root = tk.Tk()
    root.geometry("200x100")
    Example(root).pack(fill="both", expand=True)
    root.mainloop()
answer2: 回答2:

An easy way would be to pack the labels into grid, giving it column you want as needed to be on the left side / right side / center.

一个简单的方法是将标签包装成网格,给你想要的栏目,在左侧/右侧/中心。

python  tkinter