简体   繁体   English

使用 tkinter 创建两列输入字段

[英]Create two columns of entry fields using tkinter

I want to create a dynamic GUI that will change the number of input boxes based on a user input (numshoes)我想创建一个动态 GUI,它将根据用户输入(numshoes)更改输入框的数量

I have successfully done that below... however, I want two input boxes per row, right now I only have one.我在下面成功地做到了……但是,我想要每行两个输入框,现在我只有一个。

I thought self.entrys[-1].grid(row=ii, column=2) would add another column of inputs, but I still only have one.我认为self.entrys[-1].grid(row=ii, column=2)会添加另一列输入,但我仍然只有一个。

Any thoughts?有什么想法吗?

import tkinter as tk
from tkinter import *

numshoes = 6 
shoes = ['shoe1', 'shoe2', 'shoe3', 'shoe4', 'shoe5','shoe6']

master = tk.Tk()

class test:
    def __init__(self, root):
        self.variables = []
        for i in range(numshoes):
            self.variables.append(StringVar())

        self.labels = []
        self.entrys = []
        for ii in range(numshoes):
            char = str((shoes[ii]))
            self.labels.append(Label(root , text = char))
            self.labels[-1].grid(padx=0, pady=0, row=ii, column=0)
            self.entrys.append(Entry(root, textvariable =self.variables[ii]))
            self.entrys[-1].grid(padx=0, pady=0, row=ii, column=1)
            self.entrys[-1].grid(row=ii, column=2)

root = Tk()
tk.Button(root, text='Finish',command=master.quit).grid(row=(numshoes+1), column=1,sticky =tk.W, pady=4)

# root.geometry("200x600+50+50")
T = test(root)
root.mainloop()

I thought self.entrys[-1].grid(row=ii, column=2) would add another column of inputs, but I still only have one.我认为 self.entrys[-1].grid(row=ii, column=2) 会添加另一列输入,但我仍然只有一个。

No, it only moves that entry to column 2. If you want two Entry widgets on a row you have to create two Entry widgets for each row.不,它只会将该条目移动到第 2 列。如果您希望一行上有两个Entry小部件,则必须为每一行创建两个Entry小部件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM