Skip to content

Instantly share code, notes, and snippets.

@revelcw
Last active April 10, 2022 17:34
Show Gist options
  • Save revelcw/1a345ee8c8cd61edb47443534096fed6 to your computer and use it in GitHub Desktop.
Save revelcw/1a345ee8c8cd61edb47443534096fed6 to your computer and use it in GitHub Desktop.
Python Using Tkinter

Python Using Tkinter

© 2017 by Revel Carlberg West. All rights reserved.

Tkinter is way for kids to use GUIs. That creates a basic window like this:

image

with just the code:

import tkinter

window = tkinter.Tk()

and than you can add stuff to it like this:

image

but of course the button doesn't do anything because we didn't tell it to do something

import tkinter

window = tkinter.Tk()
                  #defines a button. "window" means
                  #put the button the window, and
                  #"text=" means what the button
                  #should say
button = tkinter.Button(window, text='Press Me')
button.pack()#tells the button to go on the window

so I modified it so it would do something when you click it, and that something was print "hello world"

image

and this is the code i used to make the button print hello world

import tkinter

def do_something():
    print('hello world')

window = tkinter.Tk()

button = tkinter.Button(window, text='Press Me', command=do_something)
button.pack()

Useful links

  1. Python GUI with Tkinter - 1 - Introduction
import tkinter
def do_something():
print('hello world')
window = tkinter.Tk()
button = tkinter.Button(window, text='Press Me', command=do_something)
button.pack()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment