Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22873

IslandT: Tkinter Tutorial — Create combo box widget

$
0
0

In this tutorial, let us continue with our previous example and add the combo box widget into the previously created top-level Tkinter window widget where that combo box will allow the user to select a slogan from it and then after the user has clicked on the button, both the text’s input from the text box and the selected slogan from the combo box will replace the previous text on the label widget.

import tkinter as tk
from tkinter import ttk

win =  tk.Tk()
win.title("Hello World!")

def showHello():
    #hello.configure(text=txt) # change the text inside the button
    hello_label.configure(text=helo.get() + " " + slogan.get()) # change the text inside the label

hello = ttk.Button(win, text="Hello!", command=showHello) # create a button
hello.grid(column=0, row=0)

hello_label = ttk.Label(win, text="Hello!") # create a label
hello_label.grid(column=1, row=0)

# create a text box widget
helo = tk.StringVar() # create the helo string variable object
entry_text = ttk.Entry(win, width=16, textvariable=helo) 
entry_text.grid(column=0, row=1)

# create a hello world combo box
slogan = tk.StringVar()
helo_combo = ttk.Combobox(win, width=16, textvariable=slogan, state='readonly') # the 'readonly' term will stop the user from typing value into the combo box
helo_combo['value'] = ('Hello', 'Hello Again', 'Hellowin') # create the combo box selection values
helo_combo.grid(column=1, row=1)
helo_combo.current(0)

win.mainloop()

The slogan’s StringVar object will get the value that has been selected from the user and shows it on the label widget together with the user input text from the text widget.

The above python program will produce the following output after the user has entered text into the text box widget and made the selection from the combo box widget and clicked on the button.


Viewing all articles
Browse latest Browse all 22873

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>