本文介绍了检索和使用 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 组合框选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 16:21