如何在 Tkinter 中创建选项菜单?
Tkinter包是Python的标准 GUI(图形用户界面),它为 Tk GUI 工具包提供了强大的界面。在本教程中,我们希望读者了解Python和 tkinter 模块的概念。
选项菜单
在本教程中,我们的主要重点是在Python使用 tkinter 创建选项菜单。顾名思义,选项菜单是以简单的语言为用户提供的选项列表。这可以使用 OptionMenu 类来实现,它创建一个弹出菜单并显示用户可以使用的选项。
我们可以通过初始化该类的构造函数来创建一个选项菜单。
Syntax: OptionMenu(parent, variable, choice_1, choice_2, choice_3, …)
- parent- This is a parent object which is also called as master object. It’s main role is to create the window for GUI and also keep other widgets used in the program.
- variable- This is the default name that should be shown to the option menu.
- choice_1, choice_2, choice_3, …- These are the actual list of options that are available for the user.
由于OptionMenu是一个类,所以很明显里面会有一些方法。让我们一一研究一些方法。
- get() 方法- OptionMenu 的此方法返回当前从选项菜单中选择的值。
- mainloop() 方法 - 该方法不仅是 OptionMenu 类的一部分,也是整个 tkinter 包的一部分。它的作用是在我们运行它之前组合我们程序中的所有小部件,因此在我们完成我们的代码时使用它。
- place() 方法 - 此方法将所有小部件保存在程序员指定的位置。
- pack() 方法 - 此方法有助于将 OptionMenu 排列在特定位置。
现在让我们看一些例子,这将使我们的概念更加清晰。
这是创建简单选项菜单的基本示例。
示例 1:
Python3
# Importing the tkinter module using import keyword
from tkinter import *
# Initialize parent object or master object as "parent"
parent = Tk()
# passing master object as parameter and set "COLOURS" as
# the name of the OptionMenu using set() method.
variable = StringVar(parent)
variable.set("COLOURS")
# Constructor of OptionMenu class initialized by giving
# the parameters as master object, variable name and the
# list of options in the menu.
option_menu = OptionMenu(parent, variable, "Yellow",
"Blue", "Green", "Purple",
"Black", "White")
# Using pack() method in OptionMenu class to arrange the
# option menu.
option_menu.pack()
# Using mainloop() method from OptionMenu class before we
# run the code.
parent.mainloop()
Python3
# Import tkinter using import keyword
import tkinter as tk
# set the master object in parent variable
parent = tk.Tk()
# Title for our window
parent.title("Geeksforgeeks- OptionMenu")
# Creating a Option Menu for AGE
# Set the variable for AGE and create the list
# of options by initializing the constructor
# of class OptionMenu.
Age_Variable = tk.StringVar(parent)
Age_Variable.set("Age")
Age_Option = tk.OptionMenu(parent, Age_Variable,
"below 14", "15",
"16", "17",
"above 18")
Age_Option.pack()
# Creating a Option Menu for GENDER
# Set the variable for GENDER and create the list
# of options by initializing the constructor
# of class OptionMenu.
Gender_Variable = tk.StringVar(parent)
Gender_Variable.set("Gender")
Gender_Option = tk.OptionMenu(parent,
Gender_Variable,
"Male", "Female")
Gender_Option.pack()
# Creating a Option Menu for HOBBY
# Set the variable for HOBBY and create the list
# of options by initializing the constructor
# of class OptionMenu.
Hobby_Variable = tk.StringVar(parent)
Hobby_Variable.set("Hobby")
Hobby_Option = tk.OptionMenu(parent, Hobby_Variable,
"Dance", "Code", "Sing",
"Draw")
Hobby_Option.pack()
# Combining all the widgets used in the
# program before running it
parent.mainloop()
输出:
示例 2:
蟒蛇3
# Import tkinter using import keyword
import tkinter as tk
# set the master object in parent variable
parent = tk.Tk()
# Title for our window
parent.title("Geeksforgeeks- OptionMenu")
# Creating a Option Menu for AGE
# Set the variable for AGE and create the list
# of options by initializing the constructor
# of class OptionMenu.
Age_Variable = tk.StringVar(parent)
Age_Variable.set("Age")
Age_Option = tk.OptionMenu(parent, Age_Variable,
"below 14", "15",
"16", "17",
"above 18")
Age_Option.pack()
# Creating a Option Menu for GENDER
# Set the variable for GENDER and create the list
# of options by initializing the constructor
# of class OptionMenu.
Gender_Variable = tk.StringVar(parent)
Gender_Variable.set("Gender")
Gender_Option = tk.OptionMenu(parent,
Gender_Variable,
"Male", "Female")
Gender_Option.pack()
# Creating a Option Menu for HOBBY
# Set the variable for HOBBY and create the list
# of options by initializing the constructor
# of class OptionMenu.
Hobby_Variable = tk.StringVar(parent)
Hobby_Variable.set("Hobby")
Hobby_Option = tk.OptionMenu(parent, Hobby_Variable,
"Dance", "Code", "Sing",
"Draw")
Hobby_Option.pack()
# Combining all the widgets used in the
# program before running it
parent.mainloop()
输出: