I have been trying to make a class based on Tk(). My code is such a mess, I would be embaressed to show you what I came up with. Here is what I am trying to do

class Window(Tk):
    fill in the code
    here
win = Window()
win.mainloop()

I can do this with out using classes, but as a single object I am having trouble
I would like the window to be a certain dimension, and have a single button in it. I would greatly appreciate an example.

Recommended Answers

All 3 Replies

Hi Shanenin,

Try this:

from Tkinter import *
# -- The application class
class TkBasic:
     # -- Initializes new instances of TkBasic
     def __init__(self, root):
          # Change the window geometry
          root.geometry("300x200+30+30")
          # Add the window Frame to which all widgets will be bound
          self.mainframe = Frame(root)
          self.mainframe.pack()
          # Add a Button widget (when clicked, it closes the app)
          self.button = Button(self.mainframe, text="Close",
                               command=self.close)
          self.button.pack()
     # -- Closes the application
     def close(self):
          self.mainframe.quit()
# -- The following code executes upon command-line invocation
root = Tk()
tkb = TkBasic(root)
root.mainloop()

To learn about geometry strings, go here. To learn more about making your apps look decent (layout management), go here. To learn more about Buttons and other widgets, try here.

Thank you. That will give me some ideas, you seem to do it alot differently then I was trying.

Thanks G-Do for your excellent sample code and the references. Good sample code seems to be missing with most common Python tutorials.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.