Tkinter does not work-Python 3.5

0

I'm trying to make a GUI with Python 3.5, but it gives me an error:

  

Traceback (most recent call last):     File "C: \ Users \ Juan \ Desktop \ Python \ GUI_test.py", line 2, in       class Application (Frame):     File "C: \ Users \ Juan \ Desktop \ Python \ GUI_test.py", line 13, in Application       root = Tkinter.Tk ()   NameError: name 'Tkinter' is not defined

Here the script:

from tkinter import *
class Application(Frame):

    def __init__(self,master=None):
        Frame.__init__(self,master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.myButton = Button(self, text='Button Label')
        self.myButton.grid()

    root = Tkinter.Tk()

    root.title('Frame w/ Button')
    root.geometry('200x200')

    app = Application(root)
    root.mainloop()

How can I solve this?

    
asked by JuanVan12 24.11.2016 в 11:14
source

1 answer

3

In Python 3.x the Tkinter module (as it was called in Python 2.x ) was renamed tkinter . You care about it, but when you instantiate it you leave the capital T so the interpreter does not know which module you are referring to. Anyway you use from modulo import* so the prefix is not necessary:

 root = Tkinter.Tk()

by:

 root = Tk()

On the other hand, instances your class Application within the same class (it may only be an error when formatting your code in the question), this will give you another error.

Finally the mainloop() method must be used on the application ( app ) not on the window ( root ). The code should be:

from tkinter import *
class Application(Frame):

    def __init__(self,master=None):
        Frame.__init__(self,master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.myButton = Button(self, text='Button Label')
        self.myButton.grid()

root = Tk()
root.title('Frame w/ Button')
root.geometry('200x200')

app = Application(root)
app.mainloop()
    
answered by 24.11.2016 в 11:25