ARW
ARW

Reputation: 61

Tkinter - Python 3.5

Python Programming 3rd Ed. isbn 978-1-4354-5500-9

The following code will not work for me, can anyone help me?

I'm very new to Tkinter and GUI...any advice or resources would be appreciated

Thanks, Adam

import Tkinter
from Tkinter import *

class Application(Frame):

    def __init__(self,master=None):
        super(Application, self).__init__(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()

Upvotes: 1

Views: 9808

Answers (2)

falsetru
falsetru

Reputation: 368954

In Python 3.x, Tkinter module is renamed to tkinter:

try:
    # Python 3.x
    from tkinter import *
except ImportError:
    # Python 2.x
    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)
root.mainloop()

UPDATE changed the code to run both in Python 2.x, Python 3.x.

Upvotes: 3

ARW
ARW

Reputation: 61

I found the answer....Thanks for the comments

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()

Upvotes: 0

Related Questions