如何在用户输入n的值后创建n tkinter文本小部件

2024-06-16 10:14:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过n文本框获取n个文本样本,其中n来自用户。 当我运行代码用户输入3时,应该弹出一个带有3个文本框的窗口,如何实现这一点

我的代码

#I have imported all necessary modules 
n = input("enter number of text boxes")
root1 = Tk()
root1.title("replacement text")
root1.geometry("+300+200")
textbox = list()
for i in range(n):
    textbox.append(Text(root1, height = 1, width = 57, wrap = None ))
    textbox[i].insert(INSERT,"text" +str(i) )
root1.mainloop() 

这不管用 请帮忙,谢谢😊😊


Tags: 代码text用户文本modulesinputhaveall
2条回答

你做得很好,你几乎做到了。你所需要做的就是使用geometry managers中的任何一个在窗口上定位文本小部件。此外,您没有将输入作为整数,默认情况下,任何输入都是str要将其转换为需要执行int(input(...)操作的整数

完整代码:

from tkinter import *

n = int(input("Enter number of text boxes: "))
root1 = Tk()
root1.title("replacement text")
root1.geometry("+300+200")
textbox = list()
for i in range(n):
    textbox.append(Text(root1, height = 1, width = 57, wrap = None ))
    textbox[i].insert(INSERT,"text" +str(i) )
    textbox[i].pack()

root1.mainloop() 

我真的创造了这样的东西

class NewWindowWithNControls:

def __init__(self, number_of_controls):
    self.root = Toplevel()

    windowWidth = self.root.master.winfo_reqwidth()
    windowHeight = self.root.master.winfo_reqheight()

    # Gets both half the screen width/height and window width/height
    positionRight = int(self.root.winfo_screenwidth() / 2 - windowWidth / 2)
    positionDown = int(self.root.winfo_screenheight() / 2 - windowHeight / 2)

    y_pad = "3"
    self.rows = 0

    self.root.geometry("+%s+%s" % (positionRight, positionDown - 200))
    self.root.title("Plan From File")
    self.root.configure(background=menu_background_color)

    self.input_data_list= list()

    for i in range(int(number_of_controls)):
        data_input_dict = dict()

        data_input_dict ['data_header_key'] = Entry(self.root)
        data_input_dict ['data_header_key'].configure(background=entry_box_background,
                                            foreground=text_color,
                                            width=25)
        data_input_dict ['data_header_key'].grid(row=self.rows, column=0, sticky='w', pady=y_pad)
        data_input_dict ['data_header_key'].bind('<Control-a>', select_text)
        data_input_dict ['data_header_key'].insert(0, '')

        self.rows += 1

        self.input_data_list.append(data_input_dict )

它基本上是为每一系列控件创建一个字典(如果您还希望一行中有几个文本框,如果您只需要一个,您可以跳过字典部分并将其保存在列表中),然后将其保存在一个列表中,该列表保存所有n个控件的所有信息,稍后访问此列表以从文本框中获取输入

在父窗口上,可以创建如下内容:

    self.number_of_controls_entry = Entry(self.root)
    self.number_of_controls_entry.configure(background=entry_box_background,
                                 foreground=text_color,
                                            width=8)

    self.number_of_controls_entry.grid(row=self.rows, column=1, sticky='e')
    self.number_of_controls_entry.bind('<Control-a>', select_text)
    self.number_of_controls_entry.insert(0, '')

    self.rows += 1

    self.apply_btn = Button(self.root)
    self.apply_btn.configure(background=widget_background_color, foreground=text_color, width=10)
    self.apply_btn.grid(row=self.rows, column=0, pady=15, padx=30, columnspan=2, sticky='nsew')
    self.apply_btn.configure(text='''Apply''')
    self.apply_btn.configure(command=lambda: self.apply_changes(self.number_of_controls_entry.get()))

def apply_changes(self, number_of_controls):
    self.root.destroy()
    NewWindowWithNControls(number_of_controls)

相关问题 更多 >