0
import customtkinter as ctk
import tkinter as tk
from tkinter import ttk

def get_var(input):
    input.get()

entry_var = ctk.StringVar()
entry_box = ctk.CTkEntry(
    root,
    textvariable = entry_var
    ).grid(row = 0, column = 0)

submit_button = ctk.CTkButton(
    root,
    text = "Submit entry",
    command = get_var(entry_var)
    ).grid(row = 1, column = 0)

.get() isn't recognized unless I use a stringvar from tkinter. I'm clueless as to why. Could anyone educate me and help me understand?

Is there no way to accept the argument as the tk.stringvar? or am I just doing something wrong?

JRiggles
  • 4,847
  • 1
  • 12
  • 27
Canoe
  • 9
  • 2
  • 1
    *".get() isn't recognized unless I use a stringvar from tkinter"* - what do you mean by *".get() isn't recognized"*? What did you use before *StringVar* is used? Also `command=get_var(entry_var)` will execute `get_var(entry_var)` immediately, not when the button is clicked. – acw1668 May 15 '23 at 07:33
  • I meant that def get_var(input) doesn't allow the input to use the .get() method. The .get() method is considered unknown until I use a defined StringVar. I also thought that command executes when button is clicked, I did not know that. – Canoe May 15 '23 at 08:05
  • 1
    That's why I asked in my past comment *"What did you use before StringVar is used?"*. – acw1668 May 15 '23 at 08:07
  • I'm not using anything but StringVar, but what I want to do is pass a ctk.StringVar as a parameter in the get_var() function – Canoe May 15 '23 at 09:29
  • It should be OK using `ctk.StringVar()`. What is the error when you tried to use `ctk.StringVar()`? – acw1668 May 15 '23 at 09:31
  • You are currently *calling* `get_var` when you create the button, not passing a function for the button to call. Try `CTkButton(..., command=lambda: get-var(entry_var))`, or more simply, `CTkButton(..., command=entry_var.get)`. (Note that `get_var` has to *return* the value `input.get()` produces, not simply call the function.) – chepner May 15 '23 at 13:07

1 Answers1

0

I'm not 100% familiar with CustomTkinter, but it vanilla tkinter the geometry manager methods grid, pack, and place all return None. As written, your widgets are evaluating to None and therefore won't respond to get(). Also, input is a reserved word for the input function - you should use another name here.

You also need to modify the command argument for submit_button. As written, it's going to call get_var immediately, which isn't what you want. You can avoid this by using a lambda or by modifying get_var to just call entry_var.get() directly (this is my preferred approach, since you don't have to pass in the entry_box widget).

def get_var():  # no need to pass in the widget
    return entry_var.get()  # just call 'get' directly
    # you'll probably want to do something with the value other than just 'return'
    # it, but this is an example...


entry_var = ctk.StringVar()
entry_box = ctk.CTkEntry(
    root,
    textvariable=entry_var
)
entry_box.grid(row = 0, column = 0)  # do this on a separate line

submit_button = ctk.CTkButton(
    root,
    text="Submit entry",
    command=get_var,  # pass the function by name here, no parens ()
)
submit_button.grid(row = 1, column = 0)  # do this on a separate line
JRiggles
  • 4,847
  • 1
  • 12
  • 27