Get the code for Episode 3 here
Let's explore what we can do with a simple tkinter window to configure it. When you are first thinking about building your new software, spare a little time to decide how your application is going to work for your users and what you can do to make it easier for them and a nicer interface in general.
NOTE: We are using a class in our code to show the window. Due to this we use the keyword self to refer to the 'instance' of our window. If you weren't using a class you could replace all the 'self' code with the variable name that is storing your window object, like root for example.
This will show the text in the title bar of the window
self.title("GUI with Tkinter")
You can specify how wide and high your window will be in pixels
self.geometry("300x250")
You can also specify how far from the screen edges the window will display. You can use both + and - here with the + being from the left/top screen edge to the left/top edge of the window, and - being from the right/bottom screen edge to the right/bottom edge of the window.
# 600 pixels from the left and 300 pixels from the top
self.geometry("+600+300")
Here is how you can display a window in each corner of the screen:
self.geometry("+0+0") # top left
self.geometry("+0-0") # bottom left
self.geometry("-0+0") # top right
self.geometry("-0-0") # bottom right
You can also combine the window sizing and location in the same command
self.geometry("300x250+600+300")
If the window is resizable, these commands will control how big or small it can get. They also set the size when the window is maximized.
self.minsize(width=200, height=300)
self.maxsize(width=500, height=600)
You can stop the window being resized by width, height or both
self.resizable(0, 0) # window can't be resized
If you want to display an icon in the title bar you can
self.iconbitmap("episode_3/pics/snappytk.ico")
Depending on your OS, there are other attributes you can set. To see what attributes you have available on your computer, you can print them out easily
print(self.attributes())
A tool window limits some of the things you can do with your window. It might be good for a dialog box, for example. Try it out
self.attributes("-toolwindow", 1)
If you want your window to take up the entire screen, you can do that. It won't work if you window's maxsize is set though!
self.attributes("-fullscreen", 1)
You can disable a window so that it is visible but you can't interact with it
self.attributes("-disabled", 1)
If you have multiple windows you can move one above all the others
self.attributes("-topmost", 1)
You can make windows transparent, including the title bar. 0 is fully transparent and 1 is fully opaque
self.attributes("-alpha", 0.1)
If you want to show the title bar but make the rest of the window transparent you can. Set the transparentcolor attribute to the same colour as the window's background colour
self.config(bg="green")
self.attributes("-transparentcolor", "green")
If you want a window that only shows the contents and nothing else, you can do that too
self.overrideredirect(1)
- try out all the different code above on your own window
- try moving a window around the screen to various locations
- see what will happen if you set the alpha to 0
- create your own .ico file for your app and have it display (if on windows)