我想自动选择列表框中的第一项.通过选择第一项,我并不仅仅意味着默认为第一项或设置焦点.我已经做到了这一点self.listbox.select_set(0).我想要也选择默认项目.换句话说,当我在下面运行我的代码时,我想print(value)打印默认选择的值.如果从选项菜单中选择亚洲,日本应自动打印到控制台.如果非洲,尼日利亚应该打印,德国打印到欧洲.
有关如何实现这一目标的任何建议?谢谢.
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
class App:
def __init__(self):
self.master = Tk()
self.di = {'Asia': ['Japan', 'China', 'Malaysia', 'India', 'Korea',
'Vietnam', 'Laos', 'Thailand', 'Singapore',
'Indonesia', 'Taiwan'],
'Europe': ['Germany', 'France', 'Switzerland'],
'Africa': ['Nigeria', 'Kenya', 'Ethiopia', 'Ghana',
'Congo', 'Senegal', 'Guinea', 'Mali', 'Cameroun',
'Benin', 'Tanzania', 'South Africa', 'Zimbabwe']}
self.variable_a = StringVar()
self.frame_optionmenu = ttk.Frame(self.master)
self.frame_optionmenu.pack()
options = sorted(self.di.keys())
self.optionmenu = ttk.OptionMenu(self.frame_optionmenu, self.variable_a, options[0], *options)
self.variable_a.set('Asia')
self.optionmenu.pack()
self.btn = ttk.Button(self.master, text="Submit", …Run Code Online (Sandbox Code Playgroud)