|
| 1 | +from tkinter import * |
| 2 | + |
| 3 | +def p(): |
| 4 | + print("UU") |
| 5 | + |
| 6 | + |
| 7 | +class Window(Frame): |
| 8 | + |
| 9 | + # Define settings upon initialization. Here you can specify |
| 10 | + def __init__(self, master=None): |
| 11 | + |
| 12 | + # parameters that you want to send through the Frame class. |
| 13 | + Frame.__init__(self, master) |
| 14 | + |
| 15 | + #reference to the master widget, which is the tk window |
| 16 | + self.master = master |
| 17 | + |
| 18 | + #with that, we want to then run init_window, which doesn't yet exist |
| 19 | + self.init_window() |
| 20 | + |
| 21 | + #Creation of init_window |
| 22 | + def init_window(self): |
| 23 | + |
| 24 | + # changing the title of our master widget |
| 25 | + self.master.title("GUI") |
| 26 | + |
| 27 | + # allowing the widget to take the full space of the root window |
| 28 | + self.pack(fill=BOTH, expand=1) |
| 29 | + |
| 30 | + # creating a menu instance |
| 31 | + menu = Menu(self.master) |
| 32 | + self.master.config(menu=menu) |
| 33 | + |
| 34 | + # create the file object) |
| 35 | + file = Menu(menu) |
| 36 | + |
| 37 | + # adds a command to the menu option, calling it exit, and the |
| 38 | + # command it runs on event is client_exit |
| 39 | + file.add_command(label="Exit", command=self.client_exit) |
| 40 | + |
| 41 | + #added "file" to our menu |
| 42 | + menu.add_cascade(label="File", menu=file) |
| 43 | + |
| 44 | + # create the file object) |
| 45 | + edit = Menu(menu) |
| 46 | + |
| 47 | + # adds a command to the menu option, calling it exit, and the |
| 48 | + # command it runs on event is client_exit |
| 49 | + edit.add_command(label="Undo", command=p) |
| 50 | + edit.add_command(label="Redo") |
| 51 | + |
| 52 | + #added "file" to our menu |
| 53 | + menu.add_cascade(label="Edit", menu=edit) |
| 54 | + |
| 55 | + # creating a button instance |
| 56 | + quitButton = Button(self, text="Exit",command=self.client_exit) |
| 57 | + |
| 58 | + # placing the button on my window |
| 59 | + quitButton.place(x=10, y=10) |
| 60 | + |
| 61 | + def client_exit(self): |
| 62 | + exit() |
| 63 | + |
| 64 | + |
| 65 | +# root window created. Here, that would be the only window, but |
| 66 | +# you can later have windows within windows. |
| 67 | +root = Tk() |
| 68 | + |
| 69 | +root.geometry("400x300") |
| 70 | + |
| 71 | +#creation of an instance |
| 72 | +app = Window(root) |
| 73 | + |
| 74 | +#mainloop |
| 75 | +root.mainloop() |
0 commit comments