0

I'm going to ask the user to enter a number in an entry and press the 'enter' button, then show that number of entries below on the window, here is my code, the test class makes a list of entries and it works well alone but when I combine it with the first scenario and press the button nothing happens:

from tkinter import *

root = Tk()
root.geometry("600x600+50+50")

font = ('Verdana', 10)

def enter():
    try:
        class test:
            def __init__(self, root):
                self.variables = []
                n = eval(txt_num.get())
                for i in range(n):
                    self.variables.append(StringVar())

                self.labels = []
                self.entrys = []
                mod = n % 4
                c = 0
                d = 0
                e = 0
                for ii in range(int(n/4)):
                    for jj in range(4):
                        char = c+(jj+1)
                        self.labels.append(Label(root , text = char))
                        self.labels[-1].grid(padx=0, pady=0, row=ii+d+1, column=jj)
                        self.entrys.append(Entry(root, textvariable =self.variables[c+(jj+1)]))
                        self.entrys[-1].grid(padx=0, pady=0, row=ii+d+2, column=jj)

                    d+=2
                    c+=4
                    e = ii+d+1

                for kk in range(mod):
                    char = c+(kk+1)
                    self.labels.append(Label(root , text = char))
                    self.labels[-1].grid(padx=0, pady=0, row=int(n/4)+1+e+1, column=kk)
                    self.entrys.append(Entry(root, textvariable =self.variables[c+(kk)]))
                    self.entrys[-1].grid(padx=0, pady=0, row=int(n/4)+1+e+2, column=kk)

        T = test(root)
            
    except:
        pass

lable_num = Label(font = font, text = 'Enter the number of your numbers:')
lable_num.grid(row = 0 , column = 0, columnspan = 3)

txt_num = Entry(width = 15, font = font, fg = 'gray' )
txt_num.grid(row = 0 , column = 4)

button_enter = Button(text = 'Enter' , font = font, command = enter())
button_enter.grid(row = 0 , column = 5)


root.mainloop()
ShirinJZ
  • 107
  • 2
  • 16
  • 1
    `command = enter()` calls the function *right now* (during the creation of the Button), and uses its return value (None) as the action to perform when the Button is actually clicked. You just want `command=enter`. – jasonharper Nov 21 '21 at 20:40
  • @jasonharper you mean because the function returns None, I should not use parentheses after the function name? – ShirinJZ Nov 21 '21 at 22:25
  • 1
    You want to pass *the function itself* as the `command=` option, so that the Button can call it in response to a click. – jasonharper Nov 21 '21 at 22:29
  • Got it, thank you :) – ShirinJZ Nov 21 '21 at 22:31

1 Answers1

0

The problem was calling the function command = enter() wrongly, since I have to pass the function itself as the command= option; it should be command = enter.

ShirinJZ
  • 107
  • 2
  • 16