为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

跃然一笑 2023-09-05 15:48:59
我正在创建一个需要 tkinter 组合框的应用程序。我希望组合框从应用程序启动时就具有默认值。我已经尝试过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)
查看完整描述

3 回答

?
守候你守候我

TA贡献1802条经验 获得超10个赞

调用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)


查看完整回答
反对 回复 2023-09-05
?
蓝山帝景

TA贡献1843条经验 获得超7个赞

只需设置n的值即可。

n.set('default value')


查看完整回答
反对 回复 2023-09-05
?
茅侃侃

TA贡献1842条经验 获得超21个赞

您需要禁用foreground颜色并将事件绑定到组合框。我遇到了同样的问题,上述解决方案对我不起作用。我通过将事件绑定到它来修复它。


n = tk.StringVar()

youtubechoicesLabel = ttk.Combobox(window, justify='center', textvariable=n)

youtubechoicesLabel['values'] = ("----Download Type----",

                                    "Mp4  720p",

                                    "Mp4  144p",

                                    "Video  3gp",

                                    "Audio  Mp3")


youtubechoicesLabel["background"] = '#ff0000'

#youtubechoicesLabel["foreground"] = '#ffffff' #disable it as martineau said

youtubechoicesLabel.pack(side=tk.TOP, pady=20)

youtubechoicesLabel.current(0)


#bind an event to your youtubechoicesLabel

def ComboboxEvent(event):

    print("some event")


youtubechoicesLabel.bind("<<ComboboxSelected>>", ComboboxEvent)


查看完整回答
反对 回复 2023-09-05
  • 3 回答
  • 0 关注
  • 136 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信