My entry box always returns PY_VAR1 value!!though Im using the .get function

2024/10/5 14:53:44

please take a look at my code, it's really simple I need to take the value from the entry box and use it in my program and when pressing the add button I print it ,it keeps giving me this value PY_VAR1?!! wondering what I'm doing wrong!

here's the link for t runnable code for my question My code returns an empty value for the entry box

from Tkinter import *root = Tk()content = StringVar()text = StringVar()class addcase:def mains(self):master = Tk()master.wm_title("Add")bz = Button(master,text="add",command=self.add)bz.pack()l = Label(master,text="ok")l.pack()e = Entry(master,textvariable=content)e.pack()e.focus_set()content.set("default value")text = content.get()master.mainloop()def add(me):print content.get()print text
Answer

Since you haven't actually given us a runnable example, this is really no more than a guess, but…

When you fix the code so it actually runs, then click the button, it prints this:

default value
PY_VAR1

If you look at the code, it does this:

    def add(me):print content.get()print text

The first line calls get on a StringVar that you've initialized with contents.set('default value'), and never modified again, so of course it prints out default value. As far as I can tell, you aren't surprised by this.

This second line doesn't call anything on the StringVar named text. Just printing a StringVar, instead of calling get() on it and printing the result, causes it to print the Tk name of the variable. The fact that you've also defined a local variable of the same name within addcase is irrelevant. As far as I can tell, this is what you're surprised by. But you shouldn't be. The add function can't see any local variables you've created in a completely unrelated function.

If you want a value to be shared between different methods of the same instance of a class, store them in an instance attribute, rather than a local variable.

But, more simply, if you just avoided reusing the same name for completely different variables in completely independent scopes, you would probably avoid confusing yourself, and it would be obvious what was going on.

https://en.xdnf.cn/q/120273.html

Related Q&A

Arthimatic Quiz Not Accepting Correct Answers

I am attempting to make an arithmetic quiz, but have run into this issue: Even if I input the correct answer, it seems to ignore the correct answer code and go straight to the incorrect answer code. Ba…

Linux - Check if python script is running in screen and run if not [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 1…

Making Phonebook in python : i want to get this screen by fixing my current code

I made my code like below....But as i input the data such as spam & number, previous data is deleted.So id like to make multiple value in one key... (i think using list is kinda good method)For exa…

Adding enemies to a pygame platformer

Im new to pygame and trying to make a platformer game thats based on this tutorial: http://programarcadegames.com/python_examples/show_file.php?file=platform_scroller.pyI cant quite figure out how to …

Tips for cleaning up a challenges answer? Weighted Sum of Digits [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.Improve…

Get a variable as filename from python script and use it in a batch script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 5 years ago.Improve…

Python 3.x AttributeError: NoneType object has no attribute groupdict

Being a beginner in python I might be missing out on some kind of basics. But I was going through one of the codes from a project and happened to face this :AttributeError: NoneType object has no attri…

importing images from local folder instead of using Keras/Tensorflow dataset

Hi Can someone please help me to change this code so that it wont get data from keras mnist. instead it will be getting data from local folder. where do i need to make changes in it. and where in this …

why I have negative date by subtraction of two column?

Im trying to create a column his values is the subtraction of two column but I found strange values:Patient["Waiting"] = Patient["Appointment"] - Patient["Scheduled"]Sched…