检索和使用tkinter组合框选择 [英] Retrieving and using a tkinter combobox selection

查看:87
本文介绍了检索和使用tkinter组合框选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在整理一个用于定制计算器的GUI,该计算器可以自动将某些度量单位转换为其他度量单位。

I am putting together a GUI for a customized calculator that automatically converts certain units of measurements into other units of measurement.

我想返回选择的实际文本,因此我可以根据用户选择的内容编写if语句。我该如何使python返回实际值而不是现在的值?

I want to return the actual text selected so I can write if statements from whatever the user selected. How do I get python to return the actual value instead of what I am getting now?

每当我测试此代码时,我都会收到以下内容:

Whenever I test this code I receive the following:

VirtualEvent事件x = 0 y = 0

VirtualEvent event x=0 y=0

下面是我尝试用于此过程的代码部分。对于下面的示例代码,我希望用户能够以英亩或平方英尺为单位输入面积。然后,我计划编写一条if语句,将他们选择的任何内容都转换为平方公里(此示例中未包含的数字输入代码,以保持帖子简洁)。

Below is the portion of the code that I am attempting to use for this process. For the example code below I want the user to be able to input area as either acres or square feet. I then plan to write an if statement to convert whatever they selected into square kilometers (code for input of numbers not included in this example in an attempt to keep this post concise).

import tkinter as tk
from tkinter.ttk import *

master = tk.Tk()
master.title("Gas Calculator")
v = tk.IntVar()
combo = Combobox(master)

def callback(eventObject):
    print(eventObject)

comboARU = Combobox(master)
comboARU['values']= ("Acres", "Ft^2")
comboARU.current(0) #set the selected item
comboARU.grid(row=3, column=2)
comboARU.bind("<<ComboboxSelected>>", callback)

master.mainloop()

请告诉我是否可以扩展任何内容。我还是python的新手,所以如果这只是我所缺少的简单语法,我将一点也不感到惊讶。

Please let me know if I can expand on anything. I am still new at python so I wouldnt be surprised at all if this is just a simple syntax thing that I am missing.

推荐答案

您应该使用 get()函数检索 comboARU 的内容,如下所示:

You should retrieve the content of comboARU using get() function as follows:

def callback(eventObject):
    print(comboARU.get())

这篇关于检索和使用tkinter组合框选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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