Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何更改Tkinter Python中的列数_Python_Tkinter - Fatal编程技术网

如何更改Tkinter Python中的列数

如何更改Tkinter Python中的列数,python,tkinter,Python,Tkinter,我正在用Tkinter创建一个程序,它有6个条目标签。 我很难把这些标签排在页面中央。 我不想用。打包 如何设置网格的列数?例如,当我设置column=6时,tkinter似乎忽略了 这是我的代码: ##Input 1 / Input de parametros self.PwNomeLabel1 = Label(text = "Cliente:") self.PwNomeLabel1["font"] = ("10")

我正在用Tkinter创建一个程序,它有6个条目标签。 我很难把这些标签排在页面中央。 我不想用。打包

如何设置网格的列数?例如,当我设置column=6时,tkinter似乎忽略了

这是我的代码:

               ##Input 1 / Input de parametros
            self.PwNomeLabel1 = Label(text = "Cliente:")
            self.PwNomeLabel1["font"] = ("10")
            self.PwNomeLabel1.grid(row=0,column=2,sticky=W)
            self.inputpwtest1 = Entry(borderwidth= 2, validate='key')
            self.inputpwtest1["width"] = 30
            self.inputpwtest1.grid(row=0, column=3)
            ##Input 2
            self.PwNomeLabel2 = Label(text = "Responsavel por Teste:")
            self.PwNomeLabel2["font"] = ("10")
            self.PwNomeLabel2.grid(row=1,column=2,sticky=W)
            self.inputpwtest2 = Entry(borderwidth= 2, validate='key')
            self.inputpwtest2["width"] = 30
            self.inputpwtest2.grid(row=1, column=3)
            ##Input 3
            self.PwNomeLabel3 = Label(text = "Nome do Sistema:")
            self.PwNomeLabel3["font"] = ("10")
            self.PwNomeLabel3.grid(row=2,column=2,sticky=W)
            self.inputpwtest3 = Entry(borderwidth= 2, validate='key')
            self.inputpwtest3["width"] = 30
            self.inputpwtest3.grid(row=2,column=3)
            ##Input 4
            self.PwNomeLabel4 = Label(text = "Ref:")
            self.PwNomeLabel4["font"] = ("10")
            self.PwNomeLabel4.grid(row=3,column=2,sticky=W)
            self.inputpwtest4 = Entry(borderwidth= 2, validate='key')
            self.inputpwtest4["width"] = 30
            self.inputpwtest4.grid(row=3,column=3)
            ##Input 5
            self.PwNomeLabel5 = Label(text = "Data Base:")
            self.PwNomeLabel5["font"] = ("10")
            self.PwNomeLabel5.grid(row=4,column=2,sticky=W)
            self.inputpwtest5 = Entry(borderwidth= 2, validate='key')
            self.inputpwtest5["width"] = 30
            self.inputpwtest5.grid(row=4,column=3)
            ##Input 6
            self.PwNomeLabel6 = Label(text = "Data Teste:")
            self.PwNomeLabel6["font"] = ("10")
            self.PwNomeLabel6.grid(row=5,column=2,sticky=W)
            self.inputpwtest6 = Entry(borderwidth= 2, validate='key')
            self.inputpwtest6["width"] = 30
            self.inputpwtest6.grid(row=5,column=3)

            root = Tk()
            root.title("TEST PLATFORM")
            Application(root)
            root.geometry('1366x768')
            root.mainloop()
通过这种方式,其结果:

如果我将输入结果的标签列更改为4和5:


与打印一样,Tkinter看起来也很混乱,网格保持解构状态

如果列为空,内部没有小部件,那么它就不会出现

在下面的示例中,为了使列1居中,我在列0中放置了一个宽度为100的框架。对于3列,我还有参数weight=1,因此当我展开窗口时,它以相同的方式增长

import tkinter as tk

root = tk.Tk()
for i in range(3):
    root.columnconfigure(i, weight=1)

tk.Frame(root, width=100).grid(row=0, column=0, sticky='w')
# tk.Label(root, text='Left').grid(row=0, column=0, sticky='w')
tk.Label(root, text='Center').grid(row=0, column=1)
tk.Label(root, text='Right').grid(row=0, column=2, sticky='e')

root.mainloop()

如评论中所述,您可能需要管理行/列权重

下面是一个这样做的示例,同时也展示了如何通过动态生成标签和输入字段来减少代码,而不是为所有标签和输入字段编写一堆文本

import tkinter as tk


root = tk.Tk()
root.geometry('800x500')
root.title("TEST PLATFORM")
label_list = ['Cliente:', 'Responsavel por Teste:', 'Nome do Sistema:', 'Ref:', 'Data Base:', 'Data Teste:']
entry_list = []

# Row and Column configure to manage weights
root.columnconfigure(0, weight=1)
root.columnconfigure(2, weight=1)
root.rowconfigure(0, weight=1)
root.rowconfigure(2, weight=1)

# Add a frame to hold the rest of the widgets and place that frame in the row/column without a weight.
# This will allow us to center everything that we place in the frame.
frame = tk.Frame(root)
frame.grid(row=1, column=1)

# use a loop to create our widgets.
for ndex, label in enumerate(label_list):
    tk.Label(frame, text=label, font='10').grid(row=ndex, column=0, sticky='w')
    # Store the entry widgets in a list for later use
    entry_list.append(tk.Entry(frame, borderwidth=2, width=30))
    entry_list[-1].grid(row=ndex, column=1)

# Get and print each entry value.
def print_entries():
    for entry in entry_list:
        print(entry.get())


tk.Button(frame, text='Selecionar Arquivo', command=print_entries).grid(row=len(label_list)+1, column=0, columnspan=2)
root.mainloop()

Tkinter自动折叠空的行/列。不过,您可以做一些事情来调整代码,以获得想要的效果。使用sticky和columnconfigure可能就是您想要的。也就是说,你的问题不够清楚,无法提供可靠的答案。你的目标到底是什么?你想让一切都集中起来吗?你想把东西隔开吗?具体的问题是什么?当我把column=5放在一起时,我想自由设置列,就像printscreen一样。我只是想把标签列设置在我想要的位置。你说的printscreen是什么意思?通常有一种方法可以让事物看起来像你想要的网格。当您说要自由设置列时,严格来说这是不可能的。至少不需要网格。对于小部件的静态放置,您可以使用place,但是我认为网格仍然是正确的选择,您只需要了解网格是如何工作的。下面是我写的一篇文章,解释网格是如何工作的。我发布的打印屏幕,图片,我读了这篇文章,我想这可能会对我有所帮助,我现在会尝试一些配置。你希望标签在中间吗?我尝试过,但由于任何原因都不起作用,我的窗口全是空白,标签消失了。我从来都不可能以这种方式创建小部件一个循环,但是对于我的应用程序,我不能使用这个方法,因为我需要将条目存储在一个变量中,以便使用openpyxl将其值分配给excel中的单元格。另外,这种方法对于我拥有的另一个应用程序完全有用。谢谢@LucasCruz我不认为需要将值发送到excel工作表会改变任何事情。您只需循环浏览条目列表并将值附加到excel工作表中。我总是这样做。事实上,我最近完成了一个应用程序,我的工作就是这样做的。