Follow me Python GUI Programming Series - Tkinter (4)

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

Add scrolling text box (ScrolledText) widget (Tkinter textarea)

To add a scrolling text box, you may be used ScrolledText class, as follows:

from tkinter import scrolledtext

txt = scrolledtext.ScrolledText(window,width=50,height=20)

Here we specify the scrolling text box width and height, otherwise it will fill the entire window.

from tkinter import *

from tkinter import scrolledtext

window = Tk()

window.geometry('600x400')

txt = scrolledtext.ScrolledText(window,width=50,height=10)

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

window.mainloop ()

The results are as follows:

Follow me Python GUI Programming Series - Tkinter (4) 

Setting scrolling text

To set the scrolling text may be inserted using the following method:

txt.insert (tk.INSERT, 'Your text here')

Or use

txt.insert ( "insert", 'Your text here')

txt.insert (INSERT, 'Your text here') Do not do that

example:

import tkinter as tk
from tkinter import scrolledtext

window.geometry('600x400')

txt = scrolledtext.ScrolledText(window,width=50,height=10)

window.mainloop ()

The results are as follows:

Follow me Python GUI Programming Series - Tkinter (4)

Delete / Clear scrolling text

To clear the scrolling text box content, you can use the delete method as follows:

txt.delete(1.0,END)

Create a message box

To use Tkinter displays a message box, you can use the messagebox library like this:

from tkinter import messagebox

messagebox.showinfo ( 'message header', 'message content')

It is not very easy.

Let us display a message box when the user clicks the button.

from tkinter import *

from tkinter import messagebox

window = Tk()

window.geometry('600x400')

def clicked():

    messagebox.showinfo ( 'message header', 'message content')

btn = Button (window, text = 'click here', command = clicked)

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

window.mainloop ()

When you click the button, a prompt message box is displayed.

Follow me Python GUI Programming Series - Tkinter (4)

Display warning and error messages

You can display a warning message or error message in the same way. The only change is the messagebox function

messagebox.showwarning ( 'message header', 'message content') display a warning message #

messagebox.showerror ( 'message header', 'message content') displays an error message #

Display question dialog box

Is displayed to the user / No message box messagebox can use the following functions:

from tkinter import messagebox

res = messagebox.askquestion ( 'message header', 'message content')

res = messagebox.askyesno ( 'message header', 'message content')

res = messagebox.askyesnocancel ( 'message header', 'message content')

res = messagebox.askokcancel ( 'message header', 'message content')

res = messagebox.askretrycancel ( 'message header', 'message content')

You can select the appropriate message style. showinfo function line just before the replacement line and run it.

as follows:

Follow me Python GUI Programming Series - Tkinter (4) 

Additionally, you can use the result variable to check which button was clicked.

If you click OK or yes or retry, it returns True as the value, but if you select no or cancel, it returns False.

The only function returns one of three values ​​is askyesnocancel function; it returns True or False or None.

Let's try to add more in the next section of GUI components (such as adding a SpinBox component). Stay tuned.

Guess you like

Origin www.linuxidc.com/Linux/2020-04/162797.htm