problem return option buttons tkinter

0

I CAN NOT RETURN THE VALUES OF THE 2 BUTTONS WITH A SINGLE FUNCTION

from Tkinter import *


def start():
        recuperacion= Recovery()

class Recovery():


    def __init__(self):

        global v

        recovery= Tk()
        v = IntVar()
        v.set(1)
        op1 = Radiobutton(recovery,text="Usuario",variable=v,value=1,command=self.selected)
        op2 = Radiobutton(recovery,text="Contraseña",variable=v,value=2,command=self.selected)

        op1.grid(column=2,row=2)
        op2.grid(column=2,row=3)

        recovery.mainloop()


    def selected(self):
        print v.get()



class login():
    def __init__(self):
        root= Tk()
        self.recovery=Button(root,text="Recuperar Cuenta",command=start)
        self.data_user=Entry(root)
        self.recovery.pack(side="bottom")
        root.mainloop()

ax = login()
    
asked by alex tiberiu Telegaru 05.04.2018 в 15:44
source

1 answer

0

The direct cause of your error is not to pass the parent to the variable when the instance:

v = IntVar(recovery)

Now, a few observations:

  • You should never use two instances of tkinter.Tk each with your mainloop in the same application. If you need a new window use Tkinter.Toplevel .

  • Importing with from módulo import * ( widcard imports ) is usually a bad practice and should be avoided.

  • I recommend you look at the style conventions defined in PEP-8 , especially in what refers to the way of naming classes / functions / methods / etc. They are only conventions but they help standardize your code and its readability.

  • Avoid using global variables as much as possible. You do not need to make v a global variable, the normal thing since you use POO is that it is simply an instance attribute.

Taking the above into account and restructuring your code to make use of the classes in a more natural way your code may look like this:

import Tkinter as tk   # Python 2.x
#import tkinter as tk  # Python 3.x


class Recovery(tk.Toplevel):
    def __init__(self, parent=None, *args, **kwargs):
        tk.Toplevel.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        self.value = tk.IntVar(self)
        self.value.set(2)

        op1 = tk.Radiobutton(self, text="Usuario", variable=self.value,
                             value=1, command=self.selected
                            )
        op2 = tk.Radiobutton(self, text="Contraseña", variable=self.value,
                             value=2, command=self.selected
                            )
        op1.grid(row=0, sticky=tk.W)
        op2.grid(row=1, sticky=tk.W)

    def selected(self):
        print(self.value.get())


class Login(tk.Frame):
    def __init__(self, parent=None, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        self.recovery = tk.Button(self, text="Recuperar Cuenta",
                                  command=self.recover_password
                                 )
        self.data_user = tk.Entry(self)
        self.data_user.pack(side="top")
        self.recovery.pack(side="bottom")

    def recover_password(self):
        Recovery()


if __name__ == "__main__":
    root = tk.Tk()
    Login(root).pack(side="top", fill="both", expand=True)
    root.mainloop()
    
answered by 05.04.2018 в 17:11