|
| 1 | +''' |
| 2 | +Hello and welcome to a basic intro to TKinter, which is the Python |
| 3 | +binding to TK, which is a toolkit that works around the Tcl language. |
| 4 | +
|
| 5 | +The tkinter module purpose to to generate GUIs, like windows. Python is not very |
| 6 | +popularly used for this purpose, but it is more than capable of being used |
| 7 | +
|
| 8 | +''' |
| 9 | + |
| 10 | + |
| 11 | +# Simple enough, just import everything from tkinter. |
| 12 | +from tkinter import * |
| 13 | + |
| 14 | + |
| 15 | +# Here, we are creating our class, Window, and inheriting from the Frame |
| 16 | +# class. Frame is a class from the tkinter module. (see Lib/tkinter/__init__) |
| 17 | +class Window(Frame): |
| 18 | + |
| 19 | + # Define settings upon initialization. Here you can specify |
| 20 | + def __init__(self, master=None): |
| 21 | + |
| 22 | + # parameters that you want to send through the Frame class. |
| 23 | + Frame.__init__(self, master) |
| 24 | + |
| 25 | + #reference to the master widget, which is the tk window |
| 26 | + self.master = master |
| 27 | + |
| 28 | + #with that, we want to then run init_window, which doesn't yet exist |
| 29 | + self.init_window() |
| 30 | + |
| 31 | + #Creation of init_window |
| 32 | + def init_window(self): |
| 33 | + |
| 34 | + # changing the title of our master widget |
| 35 | + self.master.title("GUI") |
| 36 | + |
| 37 | + # allowing the widget to take the full space of the root window |
| 38 | + self.pack(fill=BOTH, expand=1) |
| 39 | + |
| 40 | + # creating a button instance |
| 41 | + quitButton = Button(self, text="Exit",command=self.client_exit) |
| 42 | + |
| 43 | + # placing the button on my window |
| 44 | + quitButton.place(x=0, y=0) |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + def client_exit(self): |
| 50 | + exit() |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +# root window created. Here, that would be the only window, but |
| 59 | +# you can later have windows within windows. |
| 60 | +root = Tk() |
| 61 | + |
| 62 | +root.geometry("400x300") |
| 63 | + |
| 64 | +#creation of an instance |
| 65 | +app = Window(root) |
| 66 | + |
| 67 | +#mainloop |
| 68 | +root.mainloop() |
| 69 | + |
0 commit comments