Follow me Python GUI Programming Series - Tkinter (2)

In this tutorial series, we will learn how to develop graphical user interfaces by writing Python GUI example uses the Tkinter package.

Add button widget

Let's add a button to start the window. Create button and add a label to the window of the same way:

btn = Button (window, text = "Click here")

btn.grid(column=1, row=0)

Now, our window will look like this:

The result looks like this:

Follow me Python GUI Programming Series - Tkinter (2)

Please note that we will place it in the second column of the window (that is, 1) on. If you forget and place it on the same column (ie, 0), it displays only the buttons because the button at the top of the label.

Change button foreground and background colors

You can use fg foreground color attribute change button, or any other widget.

In addition, you can also use any bg property changes the background color of the widget.

btn = Button(window, text="点击这里", bg="orange", fg="red", font=("Arial Bold", 30))

Results as shown:

Follow me Python GUI Programming Series - Tkinter (2)

Now, if you try to click the button, nothing happens, because the event has not yet written click the button.

Handling button click event

First, we will write a function to be executed when you click the button:

def clicked():

Then, it is connected with the button specified by the following function:

btn = Button(window, text="点击这里", command=clicked, bg="orange", fg="red", font=("Arial Bold", 30))

Note that we use parentheses entered clicked rather than clicked ().

Now complete code would look like this:

def clicked():

btn = Button(window, text="点击这里", command=clicked, bg="orange", fg="red", font=("Arial Bold", 30))
btn.grid(column=1, row=0)
window.mainloop()

When we click the button, the results expected, like us, as shown below:

Follow me Python GUI Programming Series - Tkinter (2)

Use input class (the Tkinter text box) Get Input

In Python GUI previous example, we already know how to add a simple widget, now let's try to use Tkinter input class (Tkinter text box) get user input.

You can use Tkinter Entry class to create a text box, as shown below:

txt = Entry(window,width=10)

Then you can use the grid function as usual, add it to the window

The complete code is as follows:

from tkinter import *

window = Tk()

window.geometry('600x400')

lbl = Label(window, text="Linux公社", font=("Arial Bold", 30))

lbl.grid(column=0, row=0)

txt = Entry(window,width=10)

txt.grid(column=1, row=0)

def clicked():

btn = Button(window, text="点击这里", command=clicked, bg="orange", fg="red", font=("Arial Bold", 30))

btn.grid(column=2, row=0)

window.mainloop ()

Now our window like this:

Follow me Python GUI Programming Series - Tkinter (2)

Now, if you click the button, it will display the same old message, but displays the text entered on the Entry widget it?

First, you can use the get function to get input text. Therefore, we can write the following code to the function like we clicked in:

def clicked():

    res = "Welcome" + txt.get ()

    lbl.configure(text= res)

If you click the button, and the text input box, it will display the "Welcome" and together with the text input.

The following is the complete code:

from tkinter import *

window = Tk()

window.geometry('600x400')

lbl = Label(window, text="Linux公社", font=("Arial Bold", 30))

lbl.grid(column=0, row=0)

txt = Entry(window,width=20)

txt.grid(column=1, row=0)

def clicked():

    res = "Welcome" + txt.get ()

    lbl.configure(text= res)

btn = Button(window, text="点击这里", command=clicked, bg="orange", fg="red", font=("Arial Bold", 30))

btn.grid(column=2, row=0)

window.mainloop ()

The effect is as follows:

Follow me Python GUI Programming Series - Tkinter (2)

OK, cool is not cool.

Every time the code is running, we need to click the Entry widget to set the focus to write text, but how to automatically set the focus it?

txt.focus()

Also, when you run the code, you will notice Entry widget has the focus, so you can write text now to achieve.

Disable Entry widget

To disable the entry widgets may be state property to Disabled:

txt = Entry(window,width=10, state='disabled')

Now, you can not enter any text.

As shown below:

Follow me Python GUI Programming Series - Tkinter (2)

Let's try to add more in the next section of GUI widgets (for example, add a combo box widget). Stay tuned.

Guess you like

Origin www.linuxidc.com/Linux/2020-03/162774.htm