0

Please see that, I had referred to similar related answers too on this topics

1) What are the cases, that StringVar() / IntVar() would return blank / 0 value resp.?

2) Below is my smaller part of code applied to one of the functionalities in my app. Please do tell me what am I missing in my code to retrieve the expected output.

from tkinter import *

class App(Frame):
    def __init__(self,master):
        Frame.__init__(self,master)
        self.pack()
        Label(self,text="Choose timer options:").pack()
        self.delayvar = IntVar()
        self.delayradio1 = Radiobutton(self,text="Hours",variable=self.delayvar,value=1,command=self.delayfunc)
        self.delayradio1.pack()
        self.delayradio2 = Radiobutton(self,text="Minutes",variable=self.delayvar,value=2,command=self.delayfunc)
        self.delayradio2.pack()
        self.delayradio3 = Radiobutton(self,text="Seconds",variable=self.delayvar,value=3,command=self.delayfunc)
        self.delayradio3.pack()

    def delayfunc(self):
        self.delayvarvalue = self.delayvar.get()
        self.delayentryvar = StringVar()    # or IntVar()
        self.delayent = Entry(self,textvariable=self.delayentryvar)
        self.delayent.pack()
        if self.delayvarvalue == 1:
            Button(self,text="save delay",command=self.delayfunc1).pack()
        self.delayentry = self.delayent.get()   #self.delayentryvar.get()
        print(self.delayentry)

    def delayfunc1(self):
        self.delval = Text(self,height=1, width=10)
        self.delval.pack()
        self.delval.insert(END,self.delayentry)

if __name__ == '__main__' :
    root = Tk()
    App(root)
    root.title("Delay Timer")
    root.geometry("200x200")
    root.mainloop()
cs2612
  • 131
  • 1
  • 8
  • 1
    I can't tell what the problem is. Which part of this program does something unexpected? – Aran-Fey Mar 20 '18 at 11:32
  • Entry `self.delayent` is not returning the value, which I am storing in `self.delayentry` and at later point I would use this to appear on text field – cs2612 Mar 20 '18 at 11:38
  • 1
    It is returning the correct value. A newly created Entry is empty, so of course `self.delayent.get()` returns an empty string. It's not like you've given the user a chance to input any text, is it? – Aran-Fey Mar 20 '18 at 11:40

1 Answers1

1
  1. "What are the cases, that StringVar() / IntVar() would return blank / 0 value resp.?"

First of all, by return, I have to assume you mean the return of the get method on them, because if not, the answer is always and never(they always return None and that is not equal to 0) may vary respectively, as they ideally objects of the respective classes, or they fail to do so in which case I'd suspect they return None.

The cases for the both of get methods return the respective values are the same, they're either never been set('' and 0 are their default values) or they're set explicitly to the default values.

  1. Below is my smaller part of code applied to one of the functionalities in my app. Please do tell me what am I missing in my code to retrieve the expected output.

It's not obvious to me what your expected output would be but I can point some of the aspects of the code that looks troublesome.

self.delayvar is instantiated exactly as it should be and is working correctly, whereas:

self.delayentryvar currently serves no purpose, it is completely redundant. That is mainly because Entry object already has a get method that works exactly the same way as that of StringVar object's, hence if that's the heart of needing StringVar, then it has no use.

self.delayent.get()

and:

self.delayentryvar.get()

are practically the same thing as explained above and in the code OP provided will always return ''. That is because they're the returns of the current value of an Entry object that was first created almost an instant ago. The Entry isn't inserted anything either, hence they return None or ''.

Add:

self.delayent.insert('end', 'TEST')

inside delayfunc somewhere before:

self.delayentry = self.delayent.get()   #self.delayentryvar.get()

to have a better understanding of what's happening.

Nae
  • 14,209
  • 7
  • 52
  • 79
  • Understanding your points. But I have seen in many tutorials that they explicitly create a `textvariable` before they create a `Entry` Why is that so – cs2612 Mar 20 '18 at 11:55
  • @cs2612 I don't know why they would do that, perhaps take a look at this question that I've asked: [When to use Variable classes? (BooleanVar, DoubleVar, IntVar, StringVar)](https://stackoverflow.com/q/47334885/7032856) – Nae Mar 20 '18 at 11:58
  • @cs2612 it is likely they do this to prevent an error when creating the Entry field. If you have a text variable defined after the entry field is being created then you will get an error along the lines of `NameError: name 'variable_name' is not defined` – Mike - SMT Mar 20 '18 at 13:01