如何在tkinter的组合框中设置默认值?

2024-05-16 21:39:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个需要tkinter combox的应用程序。在这里,我希望组合框从应用程序开始就有一个默认值。我尝试了current()方法,但它不起作用

这是我截取的代码

n= tk.StringVar()
youtubechoicesLabel = ttk.Combobox(root, font=font, justify='center', textvariable=n)
youtubechoicesLabel['values'] = ("----Download Type----",
                                    "Mp4  720p",
                                    "Mp4  144p",
                                    "Video  3gp",
                                    "Audio  Mp3")

youtubechoicesLabel.current(0)
youtubechoicesLabel["background"] = '#ff0000'
youtubechoicesLabel["foreground"] = '#ffffff'
youtubechoicesLabel.pack(side=TOP, pady=20)

Tags: 方法代码应用程序tkinterrootcurrenttkmp4
2条回答

只需设置n的值

n.set('default value')

调用current()是正确的,并且它正在工作-由于使用指定的白色foreground颜色,您无法看到当前选择

n = tk.StringVar()
youtubechoicesLabel = ttk.Combobox(root, font=font, justify='center', textvariable=n)
youtubechoicesLabel['values'] = ("  Download Type  ",
                                    "Mp4  720p",
                                    "Mp4  144p",
                                    "Video  3gp",
                                    "Audio  Mp3")

youtubechoicesLabel.current(0)
youtubechoicesLabel["background"] = '#ff0000'
#youtubechoicesLabel["foreground"] = '#ffffff'  # <  - DISABLED
youtubechoicesLabel.pack(side=TOP, pady=20)

相关问题 更多 >