Tkinter 将 optionMenu 中选择的选项带入变量以供进一步使用 [英] Tkinter bringing chosen option from optionMenu into a variable for further use

查看:82
本文介绍了Tkinter 将 optionMenu 中选择的选项带入变量以供进一步使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在像这样在tkinter"中创建下拉菜单时:

options = ['0','1','2','3','4']option = tk.OptionMenu(menu, var, *options)var.set('选择号码')

我想确切地知道如何将用户选择的整数转换为我以后可以使用的变量.

解决方案

问题:如何取用户选择的整数

您将 options 定义为 str 的列表,因此在您的代码 中,所选选项被分配给给定的 textvariable变量.要从 str 获取 integer,请执行以下操作:

option_int = int(var.get())

<小时>

工作示例,如何获取所选OptionMenu 项的index:

导入 tkinter 作为 tk类 myOptionMenu(tk.OptionMenu):def __init__(self, parent):self.item = tk.StringVar()self.item.set("选择选项") # 默认值self.index = 无self.options = ['0.选项1.选项', '2.选项', '3.选项']super().__init__(parent, self.item, *self.options, command=self.command)self.pack()def 命令(self, v):# 循环'options'找到匹配的'item',返回索引self.index = [i for i, s in enumerate(self.options) if s == self.item.get()][0]print("def option({}), variable.get()=>{}, index:{}".format(v, self.item.get(), self.index))# >>>def option(2. Option), variable.get()=>2.选项,索引:2根 = tk.Tk()选项 = myOptionMenu(root)root.mainloop()

<块引用>

在主循环中的使用:

if option.item.get() == '2.选项':打印("选项{}被选中.".format(option.item.get()))如果 option.index == 2:打印("选项{}被选中.".format(option.index))

使用 Python 测试:3.5.3 - TkVersion:8.6

when creating a drop-down menu in "tkinter" like this:

options = ['0',
           '1',
           '2',
           '3',
           '4']

option = tk.OptionMenu(menu, var, *options)

var.set('Select number')

I want to know exactly how I can take the integer that the user has chosen and turn it into a variable that I can use later on.

解决方案

Question: how I can take the integer that the user has chosen

You define options as a list of str, therefore the chosen option are assigned to the given textvariable, in your code var. To get a integer from the str do:

option_int = int(var.get())


Working example, how to get the index of a chosen OptionMenu item:

import tkinter as tk

class myOptionMenu(tk.OptionMenu):
    def __init__(self, parent):
        self.item = tk.StringVar()
        self.item.set("Select option")  # default value
        self.index = None
        self.options = ['0. Option', '1. Option', '2. Option', '3. Option']

        super().__init__(parent, self.item, *self.options, command=self.command)
        self.pack()

    def command(self, v):
        # Loop 'options' to find the matching 'item', return the index
        self.index = [i for i, s in enumerate(self.options) if s == self.item.get()][0]
        print("def option({}), variable.get()=>{}, index:{}".format(v, self.item.get(), self.index))
        # >>> def option(2. Option), variable.get()=>2. Option, index:2

root = tk.Tk()

option = myOptionMenu(root)

root.mainloop()

Usage in the main loop:

if option.item.get() == '2. Option':
    print("Option {} is selected.".format(option.item.get()))

if option.index == 2:
    print("Option {} is selected.".format(option.index))

Tested with Python:3.5.3 - TkVersion: 8.6

这篇关于Tkinter 将 optionMenu 中选择的选项带入变量以供进一步使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆