简体   繁体   English

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

[英]How can I get the option selected by a user from a combobox in toplevel

I'm new to Python and Tkinter and was trying to create an interface to search & plot data. 我是Python和Tkinter的新手,正在尝试创建一个界面来搜索和绘制数据。 I created a very simple toplevel window to get the values from a combobox that would be selected from users. 我创建了一个非常简单的toplevel窗口,以从可从用户选择的combobox获取值。 However, I find the script would only print the first item in the list if comboxlist2.current(0) was set or it would print nothing, no matter which one is selected in the box. 但是,我发现该脚本仅在设置了comboxlist2.current(0)情况下才会打印列表中的第一项,否则无论在框中选择哪个项,它都不会打印任何内容。 I created a sample script to test this. 我创建了一个示例脚本来对此进行测试。 If I click on the "search & create", then the return values can change according to the user selection in comboxlist1 , while it would all return "1" no matter what the user selected in comboxlist2 . 如果单击“搜索并创建”,则返回值可以根据comboxlist1的用户选择进行comboxlist1 ,而无论用户在comboxlist2选择什么,返回值都将返回“ 1”。 So may I ask where is the issue and how to solve? 因此,请问问题在哪里以及如何解决?

Thanks in advance for the potential suggestions or solutions! 预先感谢您的潜在建议或解决方案!

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!

Here is the working code, which should take care of your needs. 这是工作代码,应满足您的需求。 I have removed the imports and some code snippets which are not useful. 我删除了导入和一些无效的代码段。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM