0

I want to take n text samples through n textbox, where n is taken from user. when I run code user inputs 3 then a window with 3 textbox should pop-up how to achieve this ?

my code

#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() 

this isn't working Please help, Thank you 😊😊

6
  • Hello Sachin! You can achieve it with a for loop. First, take input as an integer then iterate over the range(n) and create an n no of Entry in a new Tkinter window 🙂👍. Commented Aug 1, 2020 at 19:39
  • I have added what I have tried please check and update the changes cuz its not working as per my problem statement Commented Aug 1, 2020 at 19:48
  • input() returns a string, so you'll need to do n = int(input("enter the number of text boxes")) for starts :D Commented Aug 1, 2020 at 19:59
  • yeah and by changing n to int then textbox[i].insert(INSERT,"text" +str(i) ).pack() worked. Commented Aug 1, 2020 at 20:10
  • What does "isn't working" mean? Why do you thin kit's not working? What is it doing that is different from what you expect? Commented Aug 1, 2020 at 22:23

2 Answers 2

2

You are doing it right and you are almost there all you need to do is use any one of the geometry managers to position the text widget on the window. Also, you are not taking the input as an integer, by default any input is a str to convert it into an integer you need to do int(input(...).

Complete code:

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() 
Sign up to request clarification or add additional context in comments.

Comments

0

I've actually created something just like that!

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 )

What it does is basically creates a dictionary for each series of controls (if you want also several text boxes in one row, if you need only you one you can skip the dictionary part and just hold it in the list) and then saves it in a list that holds all the information for all the n controls, you later access this list to take the input from the text boxes

On you parent window you create something like this:

    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)

5 Comments

Classes might be a little out of scope of this question maybe?
Why is it out of scope? its more organized this way, to have a class for each window
completely agree! but given the level of this question and that OP is a new user, it might be safe to assume that introducing OOP is overkill for the current problem, and may just add more confusion than clarification. just my thoughts!
I agree with @dantechguy. Your code might be how it should be done but what OP is trying to do can be done in 5-6 lines whereas your code is of a lot more lines. It can confuse the OP.
thanks @dimakal i appreciate your effort. but as I am a beginner as of now, and I may find difficulties to understand this. but I will learn and understand this. 😉😉❤👍

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.