I want to get the text from this variable using customtkinter - python

I want to get the text from this, How do I get it?
I've instaled tkniter, customtkinter but idk how to get the text from this
entry1=customtkinter.CTkEntry(master=frame, width=220, placeholder_text='Username')

Looking into the source code for customtkinter, it seems like you can bind a customtkinter.StringVar() to the entry's textvariable and call get() on that variable
# instantiate a StringVar to store the entry contents
entry1_var = customtkinter.StringVar(value='Test')
# instantiate the entry widget
entry1 = customtkinter.CTkEntry(
master=frame,
width=220,
placeholder_text='Username',
# bind the variable to this widget
textvariable=entry1_var,
)
entry1.pack()
print(entry1_var.get())
# => Test
However, you should also be able to call get() directly on the CTkEntry widget itself without having to worry about instantiating / binding to textvariable
entry1.get()

Related

Possible to execute a command from Tkinter Entry widget?

I have a tkinter GUI which includes an Entry Widget (tk.Entry). At the moment this displays a numeric value, which can be incremented up/down by a couple of tkinter Buttons. In addition to incrementing the value displayed in the Entry Widget, whenever the Button is clicked it executes a command which updates the appropriate setting on a physical instrument with the new value from the Entry Widget.
I am wondering if it is possible at all to also have the option that if the user types a number into the Entry Widget to overwrite what is already there, that this can execute the same command as clicking the up/down buttons? For example, if the value is set to 10, and the user enters 100, then the command is executed to update the real instrument to 100.
I tried to add the code command=mycommandname to the Entry Widget (e.g. input_wavelength = tk.Entry(pwr_mtr_ctrl_frame, relief=tk.GROOVE, font=( "Ariel", 11), justify='center', validate='key', vcmd=pwr_vcmd, command=mycommandname) but get the error "unknown option "-command""
I guess this means that the Entry Widget does not have the option to execute a function, like a Button widget does? Are there ways to implement this somehow?
def print_something(*args):
print('hello world')
root = TK()
entry1 = Entry(root)
entry1.bind("<Return>", print_something)
# you can use other keys and replace it with "<Return>". EX: "f"
# by default, this function will pass an unknown argument to your function.
# thus, you have to give your function a parameter; in this case, we will use *args
root.mainloop()
According to what you described you would be better "served" by using a Spinbox.
This being said you need to bind a function to the <Return> event captured by the Entry if you want to execute something when the user enters something:
Here is how to do it:
input_wavelength = tk.Entry(pwr_mtr_ctrl_frame, relief=tk.GROOVE, font=( "Ariel", 11), justify='center', validate='key', vcmd=pwr_vcmd)
input_wavelength.bind("<Return>",mycommandname)
If mycommandname is also used as a command you need to declare it like this:
def mycommandname(event=None):
...

how to copy a string that appears on tkinter GUI

i have a password generator script that works fine, problem is, i present the password in the GUI in a label and it doesn't give me the option to copy it so i can put the password where i want it, how can i print the password to the GUI so i can copy it after, is there a better way than label?
I am using the function
tk.Label(root,text=k).grid(row=1)
k being the variable where the password is stored
Alternately if there is some python function that enables me to just straight up copy the contents of k to the clipboard that might be even better, thanks
The simplest solution is to use an Entry widget with the state set to "readonly".
Example:
import tkinter as tk
root = tk.Tk()
entry = tk.Entry(root)
entry.pack(side="top", padx=20, pady=20)
# insert the password
entry.insert(0, "SuperSecretPassw0rd!")
# configure the entry to readonly
entry.configure(state="readonly")
root.mainloop()
You can also automatically add it to the clipboard with the clipboard_clear and clipboard_append methods:
root.clipboard_clear()
root.clipboard_append(entry.get())
If I understand your problem correctly, you want to get or set a value in a TK gui? Instead of using a Label, I would use an Entry, and I would use one of the TK variable classes (such as StringVar) for k, which have get and set methods
here's a example of a script I use to get and set text values in a TK widget:
frame = tk.Frame(master)
frame.pack()
filepath = tk.StringVar()
filepath.set("/Volumes/data/data/test_data/")
fileentry = tk.Entry(frame, textvariable=filepath, width=125)
fileentry.pack()
if something:
a = filepath.get()
reference to TK variable classes: https://effbot.org/tkinterbook/variable.htm

Tkinter focus on Entry when script run

I am using tkinter to make a small user input sort of thing.
I couldn't find how to set focus to an Entry box when the script is run.
I.e. When the window opens, the first thing that automatically gets focus is the Entry box. set_focus() or focus() doesn't seem to work.
Here is my code:
root = Tk()
v = StringVar()
text = Entry(root,
textvariable=v).grid(column=0,row=0)
text.focus_set()
root.mainloop()
A geometry manager (.grid(), or .pack(), etc...) returns None. You must not use it on the same line as the assignment to a variable:
Your code was trying to call .focus_set() on a variable text whose value was set to None.
Further, the correct method to set the focus to a widget is focus_set(), not set_focus().
root = Tk()
v = StringVar()
text = Entry(root, textvariable=v)
text.grid(column=0, row=0)
text. focus_set()
root.mainloop()

Unable to configure textvaraible for Entry widget

I was making a program and part of it was:
ent = Entry(root)
and then later:
ent.config(textvarible=a)
(a is a number)
When I load the program the entry is not changed. What's wrong and how can I fix it?
Entry widgets don't work that way (Labels do). To set the value in an entry wdiget you need to delete the current contents and insert the new contents:
ent.delete(0, 'end')
ent.insert(0, a)
There is a second way of doing this. You could bind the value of the Entry to a StringVar, and then the Entry will display whatever the StringVar is set to.
var = StringVar(root)
ent = Entry(root, textvariable=var)
# to update:
var.set(a)
Also, with this method the StringVar reflects all changes the user makes to the Entry, so you need to deal with var only.

Python, Tkinter - Making get() dynamic

Below is some code that I'm testing with. In this code, I am able to create a window and have a Label on top and a Entry field on bottom. When I type in that entry field, I can dynamically change what's in the label. Now, I have included a function that is trying to evaluate a variable assigned to "tex", by storing what is predefined in the Entry widget. Which is "cat". This is picked up by:
tex = e.get()
I understand that get() is not changing dynamically as I change the text in the entry widget. So it cannot change to "dog" when I change the string in the entry widget. Is this possible? Here is the code:
from Tkinter import *
import time
root = Tk()
def change():
if tex == ("cat"):
time.sleep(0.5)
pass
else:
time.sleep(0.5)
e.delete(0, END)
e.insert(0, "dog")
v = StringVar()
e = Entry(root, textvariable=v)
e.insert(0, "cat")
e.pack(side=BOTTOM)
tex = e.get() #When text is defined with get(), it does not change
#dynamically with the entry widget
l = Label(root, textvariable=v)
l.pack(side=TOP)
change()
root.mainloop()
Any help would be appreciated.
To answer your specific question, no, there is no way for tex to magically keep updated when the entry widget changes. That feature is exactly why the textvariable attribute exists -- it causes the variable to always be updated with the value in the entry widget. If you need the data in another variable, you will have to call the .get() method.
That being said, you can set a trace on the variable, or a binding on the entry widget, and have it call code that can update tex to whatever you want when the entry widget changes.
For example, the following code shows how to have callback called whenever the value in the entry widget changes:
def callback(*args):
global tex
tex = e.get()
v.trace("w", callback)
For more information about what arguments are passed to the callback, see What are the arguments to Tkinter variable trace method callbacks?
All that being said, you have a couple of critical flaws in your code. First, you are creating the StringVar incorrectly. It needs to be v = StringVar() (note the trailing ()).
Also, you should never call sleep in the main thread of a GUI program. Read up on the after method if you want to execute some code after some time has elapsed.

Categories

Resources