Created
February 11, 2016 17:19
-
-
Save oneilsh/0271c0a861d635a0acf2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from Tkinter import * | |
import tkFileDialog | |
class MyGUI: | |
def __init__(self): | |
## Make the main window | |
self.root_window = Tk() | |
## Add a button to the main window and "grid" it | |
self.main_button = Button(self.root_window, text = "Click Me...", command = self.say_hi) | |
self.main_button.grid(row = 0, column = 0, sticky = E+W) | |
## Create a "stringVar" object that can be read/written to by GUI objects | |
self.message = StringVar(self.root_window) | |
self.message.set("Default Message") | |
## An input, which puts it input into self.message | |
self.input = Entry(self.root_window, textvariable = self.message) | |
self.input.grid(row = 1, column = 0, sticky = E+W) | |
## An output lable, text is "..." initally | |
self.out_label = Label(self.root_window, text = "...") | |
self.out_label.grid(row = 2, column = 0, pady = [20, 0]) | |
## Create a new "frame" (for a subgrid) on the right side | |
self.right_frame = Frame(self.root_window, width = 200, bg = "blue") | |
self.right_frame.grid(row = 0, column = 1, rowspan = 2) | |
## Put a label in there | |
self.right_label = Label(self.right_frame, text = "Info here") | |
self.right_label.grid(row = 0, column = 0, sticky = E+W) | |
## Note: probably we don't want to attache self.message; that's what is displyed by default | |
## Dropdowns need a command function that take an argument | |
self.dropdown = OptionMenu(self.right_frame, self.message, "One", "Two", "Three", command = self.say_hi_with_param) | |
self.dropdown.grid(row = 1, column = 0, sticky = E+W) | |
## This might be better, another one in the right frame | |
## Here we're using a command function that takes an optional argument so it can work with both a dropdown and a button | |
self.dropdown2 = OptionMenu(self.right_frame, "Pick one:", "One", "Two", "Three", command = self.say_hi_with_optional_param) | |
self.dropdown2.grid(row = 2, column = 0, sticky = E+W) | |
## And a button in the right frame | |
## Here we're using a command function that takes an optional argument so it can work with both a dropdown and a button | |
self.sub_button = Button(self.right_frame, text = "Click ME!", command = self.say_hi_with_optional_param) | |
self.sub_button.grid(row = 3, column = 0, sticky = E+W) | |
## Configure the columns of the main grid so they can "stretch" | |
self.root_window.columnconfigure(0, weight = 1) | |
self.root_window.columnconfigure(1, weight = 4) | |
## cause the main window to loop/display | |
self.root_window.mainloop() | |
## A function that reads the contents of the self.message variable and prints it, | |
## and puts the contents in the self.out_label | |
def say_hi(self): | |
message = self.message.get() | |
self.out_label["text"] = "Hello there ;) " + message | |
print("Hello there ;) " + message) | |
## A function that takes a parameter and puts it in the | |
## self.out_label | |
def say_hi_with_param(self, inputmessage): | |
self.out_label["text"] = inputmessage | |
## A function that takes an optional parameter and puts it in the | |
## self.out_label, if no parameter is given, it gets the message from self.message | |
def say_hi_with_optional_param(self, inputmessage = None): | |
if inputmessage == None: | |
message = self.message.get() | |
else: | |
message = inputmessage | |
self.out_label["text"] = message | |
# create an instance of the class | |
mygui = MyGUI() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment