Writing a graphical shell in Python - part 1
Hello Python lovers!
You all know perfectly well that programs are written both in the console version and with a graphical GUI shell.
GUI - graphical user interface
Since I have not noticed any programs with GUI on the forum, I decided to give some basic lessons.
There are admirers of both the console options and the GUI. And the question which is better is completely wrong. They always try to do the most complex programs with the shell, otherwise the software will acquire a wild number of options that are difficult to understand. In addition, if the program is written not for personal use, then you need to take into account that most of the users are sitting under Windows OS and are used to graphics. Therefore, even if you are an ardent supporter of the console, but for example, you make programs to order, then you must be able to write programs with a shell.
There are many Python GUI options. Here is an incomplete list:
Tkinter
PyGTK
PySide
PyQt wxPython We will be making the interface using Tkinter. Its main advantage is its built-in Python. That is, you do not need to install any third-party modules, libraries and other software. The disadvantages include a less presentable appearance compared to other frameworks and wrappers. Let's start with a blank, which will be the main skeleton on which you can throw anything you want.
Run it and get such a window
Now let's go through the code and figure out what is written there.
from tkinter import * - we import tkinter, and the asterisk icon means that we will be provided with all the library's capabilities. You can write differently, importing only what will be used in the program, for example: from tkinter import Tk, Label But it is not very convenient to list the necessary widgets separated by commas.
root = Tk () - instead of root, you can write anything you want, and Tk () is a library of the tkinter module, which is a kind of instruction processor for the Tcl language.
root.title ("My first graphical Python program")- already from the name it is clear that title is a title in which you can write the name of the program, version.
root.geometry ("400x250") - the size of the program window. By default, the program runs anywhere on the monitor, but tends to the upper left corner of the screen. We can set the place where the program should be displayed. To do this, add X and Y coordinates to the width and height. "When launched, the window will be 300 pixels to the right and 250 pixels down from the top left corner of the screen. Root.geometry (" 400x250 + 300 + 250 ")
root. resizable (width = False, height = False) - in most cases we do not need the program to resize the window, and with the False parameters we prohibit resizing the width and
height.root.- the mainloop method starts the entire event loop, without it the graphical shell will not start.
Well, we figured out the launch of the main window. Now is the time to move on. In the graphical interface, there are such widgets and elements as Button, Label, Entry, Radiobutton, Checkbutton and others. But all this stuff needs to be somehow positioned in the main window of the program. There are pack (), place () and grid () methods for this .
The simplest and most popular pack () method is used for the most simple programs. The place () method is almost never used, and the grid () method is the most complex and most accurate. It has the most options and allows you to arrange the elements exactly as you intended.
Let's start with the simplest of course - the pack () method.
Add a button to our program
And now we have a button
btn1 = Button (text = "BUTTON", background = "# 000000", foreground = "# fff", padx = "15", pady = "2", font = "12") Here we created a Button named btn1, wrote the text BUTTON on the button, and set the colors for the text and background of the button. Also set the indents from the text along the x and y axes, and the font size. You can change the size of a font, but also the font style itself. For example, put font = "Arial 10" and compare the results.
In this case, the button width was equal to the content + padding. But the button can also be set to width and height. Try adding values to the code width = 12, height = 4
btn1.pack () - this is where our button was packed and it became visible
If we have not explicitly indicated where the button should be located, then it accepts the following parameter btn1.pack (side = TOP) by default. Side 4 can have them all - BOTTOM, TOP, LEFT, RIGHT
Label widget or text label. Let's add it to our code
Please note - the label is added above the button, so it is displayed above the button. It is worth swapping them, and in the window they also swap places, try it.
A text label, like a button, can take values in long and short versions bg = background
Make a shell as in the screenshot
You all know perfectly well that programs are written both in the console version and with a graphical GUI shell.
GUI - graphical user interface
Since I have not noticed any programs with GUI on the forum, I decided to give some basic lessons.
There are admirers of both the console options and the GUI. And the question which is better is completely wrong. They always try to do the most complex programs with the shell, otherwise the software will acquire a wild number of options that are difficult to understand. In addition, if the program is written not for personal use, then you need to take into account that most of the users are sitting under Windows OS and are used to graphics. Therefore, even if you are an ardent supporter of the console, but for example, you make programs to order, then you must be able to write programs with a shell.
There are many Python GUI options. Here is an incomplete list:
Tkinter
PyGTK
PySide
PyQt wxPython We will be making the interface using Tkinter. Its main advantage is its built-in Python. That is, you do not need to install any third-party modules, libraries and other software. The disadvantages include a less presentable appearance compared to other frameworks and wrappers. Let's start with a blank, which will be the main skeleton on which you can throw anything you want.
Python:
from tkinter import *
root = Tk()
root.title("Моя первая графическая программа на Python")
root.geometry("400x250")
root.resizable(width=False, height=False)
root.mainloop()
Now let's go through the code and figure out what is written there.
from tkinter import * - we import tkinter, and the asterisk icon means that we will be provided with all the library's capabilities. You can write differently, importing only what will be used in the program, for example: from tkinter import Tk, Label But it is not very convenient to list the necessary widgets separated by commas.
root = Tk () - instead of root, you can write anything you want, and Tk () is a library of the tkinter module, which is a kind of instruction processor for the Tcl language.
root.title ("My first graphical Python program")- already from the name it is clear that title is a title in which you can write the name of the program, version.
root.geometry ("400x250") - the size of the program window. By default, the program runs anywhere on the monitor, but tends to the upper left corner of the screen. We can set the place where the program should be displayed. To do this, add X and Y coordinates to the width and height. "When launched, the window will be 300 pixels to the right and 250 pixels down from the top left corner of the screen. Root.geometry (" 400x250 + 300 + 250 ")
root. resizable (width = False, height = False) - in most cases we do not need the program to resize the window, and with the False parameters we prohibit resizing the width and
height.root.- the mainloop method starts the entire event loop, without it the graphical shell will not start.
Well, we figured out the launch of the main window. Now is the time to move on. In the graphical interface, there are such widgets and elements as Button, Label, Entry, Radiobutton, Checkbutton and others. But all this stuff needs to be somehow positioned in the main window of the program. There are pack (), place () and grid () methods for this .
The simplest and most popular pack () method is used for the most simple programs. The place () method is almost never used, and the grid () method is the most complex and most accurate. It has the most options and allows you to arrange the elements exactly as you intended.
Let's start with the simplest of course - the pack () method.
Add a button to our program
Python:
from tkinter import *
root = Tk()
root.title("Моя первая графическая программа на Python")
root.geometry("400x250+300+250")
root.resizable(width=False, height=False)
btn1 = Button(text="BUTTON", background="#000000", foreground="#fff", padx="15", pady="2", font="12")
btn1.pack()
root.mainloop()
btn1 = Button (text = "BUTTON", background = "# 000000", foreground = "# fff", padx = "15", pady = "2", font = "12") Here we created a Button named btn1, wrote the text BUTTON on the button, and set the colors for the text and background of the button. Also set the indents from the text along the x and y axes, and the font size. You can change the size of a font, but also the font style itself. For example, put font = "Arial 10" and compare the results.
In this case, the button width was equal to the content + padding. But the button can also be set to width and height. Try adding values to the code width = 12, height = 4
btn1.pack () - this is where our button was packed and it became visible
If we have not explicitly indicated where the button should be located, then it accepts the following parameter btn1.pack (side = TOP) by default. Side 4 can have them all - BOTTOM, TOP, LEFT, RIGHT
Label widget or text label. Let's add it to our code
Python:
label = Label(text="Привет Codeby!", font="12", pady="10")
label.pack()
btn1 = Button(text="BUTTON", background="#000000", foreground="#fff", padx="15", pady="2", font="12")
btn1.pack()
A text label, like a button, can take values in long and short versions bg = background
- bg / background: background color
- fg / foreground: text color
Make a shell as in the screenshot
Commentaires