Tkinter. Drop-down list and combo box

OptionMenu drop-down list

grammar

OptionMenu(parent object, options, *value)
*value is a series of drop-down lists

Simple application

from tkinter import *
from tkinter import messagebox
class Application(Frame):
    def __init__(self,master=None):
        super().__init__(master)
        self.master=master
        self.pack()
        self.createWidget()

    def createWidget(self):
        var=StringVar(root)
        self.optionmenu=OptionMenu(self,var,"离散数学","线性代数","计算机网络","大学英语","计算机组成原理").pack()
if __name__ == '__main__':
    root=Tk()
    root.geometry('300x200')
    root.title('萤火虫')
    app=Application(master=root)
    root.mainloop()

Insert picture description here
Click once.
Insert picture description here
Because it is not convenient to use the above method when there are too many items, it is more appropriate to use tuples to build a list.

Insert picture description here
The effect is the same.
Set the default options below.
Tuple variable name + index
Insert picture description here
Get the content of the option.
Use the get method learned earlier

Insert picture description here
Click to confirm selection:
Insert picture description here

Combobox combo box

Simple application

from tkinter import *
from tkinter.ttk import *
class Application(Frame):
    def __init__(self,master=None):
        super().__init__(master)
        self.master=master
        self.pack()
        self.createWidget()

    def createWidget(self):
        data=("离散数学","线性代数","计算机网络","大学英语","计算机组成原理")
        self.var=StringVar(root)
        self.var.set(data[0])#也可使用self.var.current(0)
        self.cb=Combobox(self,textvariable=self.var,value=data).pack()
if __name__ == '__main__':
    root=Tk()
    root.geometry('300x200')
    root.title('萤火虫')
    app=Application(master=root)
    root.mainloop()

Insert picture description here
Default discrete mathematics
Insert picture description here
Get current options
Insert picture description here
Click to confirm
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44862120/article/details/108028205