如何从顶级组合框中获取用户选择的选项

Yizhao Chen

我是Python和Tkinter的新手,正试图创建一个界面来搜索和绘制数据。我创建了一个非常简单的toplevel窗口combobox,可从将要从用户选择的值中获取值但是,我发现该脚本仅在comboxlist2.current(0)设置了列表的情况才打印列表中的第一项,否则无论在框中选择哪个项,都不会打印任何内容。我创建了一个示例脚本来对此进行测试。如果单击“搜索并创建”,则返回值可以根据中的用户选择进行更改comboxlist1,而无论用户在中选择什么,返回值都将返回“ 1” comboxlist2因此,请问问题在哪里以及如何解决?

预先感谢您的潜在建议或解决方案!

import tkinter as tk
from tkinter import ttk
from tkinter import *

def root_print():    
    reg_in = comboxlist1.get()
    print(reg_in)   #print the value selected

def on_click():
    tl = Toplevel()
    comvalue2 = tk.StringVar()
    comboxlist2 = ttk.Combobox(tl,textvariable=comvalue2)
    comboxlist2["values"] = ("1","2","3")
    comboxlist2.grid()
    comboxlist2.current(0)  #select the first one as default
    #mm = comboxlist2.get()
    #print(mm)              #print directly
    go(comboxlist2,tl)
    tl.wait_window()
    return

def go(comboxlist2,tl):
    mm = comboxlist2.get()
    Button(tl,text='go', command=lambda:test(mm)).grid()

def test(mm):
    print(mm)       #do the same thing for the comboxlist2

root = Tk()
root.title('search')      #create an interface
root.geometry('+400+200')       #size and position

Label(text='region    ').grid(row=2,column=0)
comvalue1 = tk.StringVar()
comboxlist1=ttk.Combobox(root,textvariable=comvalue1) 
comboxlist1["values"]=("all","africa","asia","australia","canada","europe","mexico","southamerica","usa")
comboxlist1.grid(row=2,column=1)
comboxlist1.current(0)
Button(text='search & create', command=root_print).grid(row=0,column=4)
Button(text='click', command=on_click).grid(row=1, column=4)
loop = mainloop()#go!
Miraj50

这是工作代码,应满足您的需求。我删除了导入和一些无效的代码段。

import tkinter as tk
from tkinter import ttk

def root_print():
    reg_in = comboxlist1.get()
    print(reg_in)

def on_click():
    tl = tk.Toplevel()
    comvalue2 = tk.StringVar()
    comboxlist2 = ttk.Combobox(tl,textvariable=comvalue2)
    comboxlist2["values"] = ("1","2","3")
    comboxlist2.grid()
    comboxlist2.current(0)  #select the first one as default
    tk.Button(tl,text='go', command=lambda: test(comboxlist2.get())).grid()
    tl.wait_window()

def test(mm):
    print(mm)

root = tk.Tk()
root.title('search')      #create an interface
root.geometry('+400+200')       #size and position

tk.Label(text='region    ').grid(row=2,column=0)
comvalue1 = tk.StringVar()
comboxlist1=ttk.Combobox(root,textvariable=comvalue1) 
comboxlist1["values"]=("all","africa","asia","australia","canada","europe","mexico","southamerica","usa")
comboxlist1.grid(row=2,column=1)
comboxlist1.current(0)
tk.Button(text='search & create', command=root_print).grid(row=0,column=4)
tk.Button(text='click', command=on_click).grid(row=1, column=4)
root.mainloop()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在警报对话框中获取用户选择

如何在Android弹出对话框中获取用户选择的字符串值?

在组合框中获取所有用户选择的值

组合框中的字典。如何从所选选项中获取价值?

如何在Tkinter组合框中获取选定选项的索引

如何在组合框中显示用户选择的值?

WPF 如何取消用户在组合框中的选择?

在Spring MVC / JSP / Controller中从列表框获取用户的选择

如何从选择框中的选项中获取属性值

在组合框中获取所选项目

如何在选择框中获取所选选项的值

如何从嵌套请求中获取用户?

获取用户在 selenium 中查看的选项卡

R Shiny:如何从DT数据表的搜索框中获取用户输入?

在线Dynamics CRM 2011:如何从插件中的对话框获取用户响应

如何从Visual Basic的列表框中获取用户输入?

如何从jquery ui对话框中获取用户输入

如何使特定选项从组合框中消失?

如何从python Flask WTF中的SelectField获取用户选择的值?

如何在Windows下的Notebook控件中获取用户的新页面选择?

如何从qdateEdit获取用户输入并从postgres中的数据库中选择它

如何在 Microsoft Word Online 文档中获取用户选择

从“覆盖”对话框中获取用户价值

从mvc文本框中获取用户输入数据

在取消时也从提示框中获取用户评论

从选择框中获取选定的选项 html

如何获取用户在 CloudFormation 中选择的 VPC 的 CIDR 块?

如何获取用户从样本中选择的值?

MS Access Forms:如何在组合框中动态更改选择选项?