Home > Blockchain >  How would I get multiple Inputs from entry on a button press, Tkinter?
How would I get multiple Inputs from entry on a button press, Tkinter?

Time:11-27

I'm working on a project for a friend where I need to make 12 entries and have them saved as a xml file when I press the button, but I keep getting it to duplicate the input to the other boxes and only print it once.

import tkinter as tk
from tkinter import *
from tkinter.ttk import *


#                               GUI
#---------------------------------------------------------------
#

# Root Setup and size
root = tk.Tk()
root.geometry('900x500')
root.title('Box Designer')

# Top Label
label = tk.Label(root, text = 'Please Enter Parameters', font = ('Arial', 18))
label.pack(padx= 20, pady= 20)


# Submit Button

sv = StringVar()
def callback():
    print(sv.get())
    return True

button_border = tk.Frame(root, highlightbackground = "black", highlightthickness = 2, bd=0,)
donebtn = tk.Button(button_border, text = 'Submit', fg = 'black', font = (15),default="active", command=callback)
root.bind('<Return>', lambda e: donebtn.invoke())





# Entry Boxes for X1-12

#setup columns
entryframe = tk.Frame(root) 
entryframe.columnconfigure(0)
entryframe.columnconfigure(1)

#X1
entry1 = tk.Entry(entryframe, textvariable= sv)
entry1.grid(row=0, column = 1, sticky = tk.W tk.E)
entry1Label = tk.Label(entryframe, text= 'X1:')
entry1Label.grid(row=0, column = 0, sticky = tk.W tk.E)

# X2
entry2 = tk.Entry(entryframe, textvariable= sv)
entry2.grid(row=0, column = 3, sticky = tk.W tk.E)
entry2Label = tk.Label(entryframe, text= 'X2:')
entry2Label.grid(row=0, column = 2, sticky = tk.W tk.E)




# Packs
entryframe.pack(side= 'bottom', fill= 'x', pady= 60)
donebtn.pack()
button_border.pack()
root.mainloop()

I'm trying to use a StringVar to record the inputs then print them with a callback on my button

CodePudding user response:

As mentioned in the comment in your question, your code with added stringvars and modified callback:

import tkinter as tk
from tkinter import *
from tkinter.ttk import *

#                               GUI
# ---------------------------------------------------------------
#

# Root Setup and size
root = tk.Tk()
root.geometry('900x500')
root.title('Box Designer')

# Top Label
label = tk.Label(root, text='Please Enter Parameters', font=('Arial', 18))
label.pack(padx=20, pady=20)


# Submit Button

def callback():
    for idx, field in enumerate((field1, field2), 1):
        print(f'field{idx}: {field.get()}')
    return True


button_border = tk.Frame(root, highlightbackground="black", highlightthickness=2, bd=0, )
donebtn = tk.Button(button_border, text='Submit', fg='black', font=(15), default="active", command=callback)
root.bind('<Return>', lambda e: donebtn.invoke())

# Entry Boxes for X1-12

# setup columns
entryframe = tk.Frame(root)
entryframe.columnconfigure(0)
entryframe.columnconfigure(1)

# X1
field1 = StringVar(root)
entry1 = tk.Entry(entryframe, textvariable=field1)
entry1.grid(row=0, column=1, sticky=tk.W   tk.E)
entry1Label = tk.Label(entryframe, text='X1:')
entry1Label.grid(row=0, column=0, sticky=tk.W   tk.E)

# X2
field2 = StringVar(root)
entry2 = tk.Entry(entryframe, textvariable=field2)
entry2.grid(row=0, column=3, sticky=tk.W   tk.E)
entry2Label = tk.Label(entryframe, text='X2:')
entry2Label.grid(row=0, column=2, sticky=tk.W   tk.E)

# Packs
entryframe.pack(side='bottom', fill='x', pady=60)
donebtn.pack()
button_border.pack()
root.mainloop()
  • Related