OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Taking Values from tkinter input boxes to be used as input variables in another function

  • Thread starter Thread starter stanley1O1
  • Start date Start date
S

stanley1O1

Guest
I am currently working on building a data analysis program for my research, and just trying to make it more user-friendly for the undergrads in my lab.

I am trying to take the user input of several textboxes to be used as variables for reading a csv file and pulling data from it. Essentially I am asking the user "which columns in the csv do you want to pull" and taking that input to pull data from said column.

It currently works if I manually input which headers I need:

Code:
def plot_eye_data(eye_data, fig):
    fig.clear()  # Clear previous plots

    ax1 = fig.add_subplot(311)

    # Plot Left Eye
    ax1.plot(eye_data['Timestamp'], eye_data['LeftGazeX'], label='LGaze X')
    ax1.plot(eye_data['Timestamp'], eye_data['LeftGazeY'], label='LGaze Y')
    ax1.set_xlabel('Timestamp')
    ax1.set_ylabel('Gaze Position')
    ax1.set_title('Left Eye Gaze Position')
    ax1.legend()
    ax1.grid(True)

But I am not sure where I am going wrong when using Tkinter to get inputs.

Code:
# graph fxn
def plot_eye_data(eye_data, fig, time, leftx, lefty):
    fig.clear()  # Clear previous plots
    
    ax1 = fig.add_subplot(311)

    # Plot Left Eye
    ax1.plot(eye_data[time], eye_data[leftx], label='LGaze X')
    ax1.plot(eye_data[time], eye_data[lefty], label='LGaze Y')
    ax1.set_xlabel('Timestamp')
    ax1.set_ylabel('Gaze Position')
    ax1.set_title('Left Eye Gaze Position')
    ax1.legend()
    ax1.grid(True)
# button fxns
def leftx_name():
    global leftx
    leftx = LeftX_Head_entry.get()
    return leftx
def lefty_name():
    global lefty
    lefty = LeftY_Head_entry.get()
    return lefty
def time_name():
    global time
    time = Time_Head_Entry.get()
    return time
# Initialize the main window
root = tk.Tk()
root.title("Eye Data Classification")
# Left side - Controls, File Input Selection, and Text

# data selection window
data_select = tk.Toplevel(root)
data_select.transient(root)
data_select.maxsize(500, 500)

input_folder_label = tk.Label(data_select, text="Input Folder:")
input_folder_label.grid(row=0, column=0, padx=5, pady=5, sticky="w")
input_folder_entry = tk.Entry(data_select, width=20)
input_folder_entry.grid(row=0, column=1, columnspan= 1, padx=5, pady=5)
tk.Button(data_select, text="Browse", command=browse_input_folder).grid(row=0, column=2, padx=5, pady=5)
LeftX_Head_input = tk.Label(data_select, text="LeftX Header")
LeftX_Head_input.grid(row=1, column=0, padx=5, pady=5, sticky="w")
LeftX_Head_entry = tk.Entry(data_select, width=20)
LeftX_Head_entry.grid(row=1, column=1, columnspan= 2, padx=5, pady=5)

LeftY_Head_input = tk.Label(data_select, text="LeftY Header")
LeftY_Head_input.grid(row=2, column=0, padx=5, pady=5, sticky="w")
LeftY_Head_entry = tk.Entry(data_select, width=20)
LeftY_Head_entry.grid(row=2, column=1, columnspan= 2, padx=5, pady=5)

Time_Head_input = tk.Label(data_select, text="Time Header")
Time_Head_input.grid(row=5, column=0, padx=5, pady=5, sticky="w")
Time_Head_Entry = tk.Entry(data_select, width=20)
Time_Head_Entry.grid(row=5, column=1, columnspan= 2, padx=5, pady=5)
tk.Button(data_select, width = 5, text="Pull Data", command=lambda: [handle_pull_data(), rightx_name(), righty_name(),lefty_name(),leftx_name(),time_name()]).grid(row=6, column=1, pady=5, sticky="ew")

There is other inputs (hence the tkinter window will look yucky)

I was expecting it to be able to grab the contents of said textbox, then use that value in pulling the data from the csvs and graphing it, but it just tells me the variables in the plot_eye_data are undefined. When telling it to print the contents of a textbox it can:

Code:
print(leftx)

But won't use that variable in the graph. However hard-coding that variable:

Code:
leftx = LeftEyeX

Correctly grabs the column.
<p>I am currently working on building a data analysis program for my research, and just trying to make it more user-friendly for the undergrads in my lab.</p>
<p>I am trying to take the user input of several textboxes to be used as variables for reading a csv file and pulling data from it. Essentially I am asking the user "which columns in the csv do you want to pull" and taking that input to pull data from said column.</p>
<p>It currently works if I manually input which headers I need:</p>
<pre><code>def plot_eye_data(eye_data, fig):
fig.clear() # Clear previous plots

ax1 = fig.add_subplot(311)

# Plot Left Eye
ax1.plot(eye_data['Timestamp'], eye_data['LeftGazeX'], label='LGaze X')
ax1.plot(eye_data['Timestamp'], eye_data['LeftGazeY'], label='LGaze Y')
ax1.set_xlabel('Timestamp')
ax1.set_ylabel('Gaze Position')
ax1.set_title('Left Eye Gaze Position')
ax1.legend()
ax1.grid(True)
</code></pre>
<p>But I am not sure where I am going wrong when using Tkinter to get inputs.</p>
<pre><code>
# graph fxn
def plot_eye_data(eye_data, fig, time, leftx, lefty):
fig.clear() # Clear previous plots

ax1 = fig.add_subplot(311)

# Plot Left Eye
ax1.plot(eye_data[time], eye_data[leftx], label='LGaze X')
ax1.plot(eye_data[time], eye_data[lefty], label='LGaze Y')
ax1.set_xlabel('Timestamp')
ax1.set_ylabel('Gaze Position')
ax1.set_title('Left Eye Gaze Position')
ax1.legend()
ax1.grid(True)
# button fxns
def leftx_name():
global leftx
leftx = LeftX_Head_entry.get()
return leftx
def lefty_name():
global lefty
lefty = LeftY_Head_entry.get()
return lefty
def time_name():
global time
time = Time_Head_Entry.get()
return time
# Initialize the main window
root = tk.Tk()
root.title("Eye Data Classification")
# Left side - Controls, File Input Selection, and Text

# data selection window
data_select = tk.Toplevel(root)
data_select.transient(root)
data_select.maxsize(500, 500)

input_folder_label = tk.Label(data_select, text="Input Folder:")
input_folder_label.grid(row=0, column=0, padx=5, pady=5, sticky="w")
input_folder_entry = tk.Entry(data_select, width=20)
input_folder_entry.grid(row=0, column=1, columnspan= 1, padx=5, pady=5)
tk.Button(data_select, text="Browse", command=browse_input_folder).grid(row=0, column=2, padx=5, pady=5)
LeftX_Head_input = tk.Label(data_select, text="LeftX Header")
LeftX_Head_input.grid(row=1, column=0, padx=5, pady=5, sticky="w")
LeftX_Head_entry = tk.Entry(data_select, width=20)
LeftX_Head_entry.grid(row=1, column=1, columnspan= 2, padx=5, pady=5)

LeftY_Head_input = tk.Label(data_select, text="LeftY Header")
LeftY_Head_input.grid(row=2, column=0, padx=5, pady=5, sticky="w")
LeftY_Head_entry = tk.Entry(data_select, width=20)
LeftY_Head_entry.grid(row=2, column=1, columnspan= 2, padx=5, pady=5)

Time_Head_input = tk.Label(data_select, text="Time Header")
Time_Head_input.grid(row=5, column=0, padx=5, pady=5, sticky="w")
Time_Head_Entry = tk.Entry(data_select, width=20)
Time_Head_Entry.grid(row=5, column=1, columnspan= 2, padx=5, pady=5)
tk.Button(data_select, width = 5, text="Pull Data", command=lambda: [handle_pull_data(), rightx_name(), righty_name(),lefty_name(),leftx_name(),time_name()]).grid(row=6, column=1, pady=5, sticky="ew")
</code></pre>
<p>There is other inputs (hence the tkinter window will look yucky)</p>
<p>I was expecting it to be able to grab the contents of said textbox, then use that value in pulling the data from the csvs and graphing it, but it just tells me the variables in the plot_eye_data are undefined. When telling it to print the contents of a textbox it can:</p>
<pre><code>print(leftx)
</code></pre>
<p>But won't use that variable in the graph. However hard-coding that variable:</p>
<pre><code>leftx = LeftEyeX
</code></pre>
<p>Correctly grabs the column.</p>
 

Latest posts

Top