Python 从Tkinter组合框中检索值

Python 从Tkinter组合框中检索值,python,tkinter,Python,Tkinter,最终,我想将组合框中的值用作其他函数中的参数,但我认为,如果我现在就可以打印它们,那就足够了。这是我到目前为止所拥有的 import tkinter as tk from tkinter import ttk import time def ok(): betType = betTypeVar.get() season = seasonVar.get() print(betType, season) def CreateSimPreviousSeasonWindow(

最终,我想将组合框中的值用作其他函数中的参数,但我认为,如果我现在就可以打印它们,那就足够了。这是我到目前为止所拥有的

import tkinter as tk
from tkinter import ttk
import time

def ok():
    betType = betTypeVar.get()
    season = seasonVar.get()
    print(betType, season)

def CreateSimPreviousSeasonWindow():

    prevSeasonWindow = tk.Tk()

    #============= Bet Type Input =============#
    betTypeVar = tk.StringVar()

    betTypeLabel = tk.Label(prevSeasonWindow, text="Bet type:").grid(row=0,column=0)
    betTypeChosen = ttk.Combobox(prevSeasonWindow, values=['Moneyline','Total'])
    betTypeChosen.grid(row=0, column=1)

    seasonVar = tk.StringVar()
    seasonLabel = tk.Label(prevSeasonWindow, text='Season:').grid(row=1, column=0)
    seasonChosen = ttk.Combobox(prevSeasonWindow, values=['2018', '2017'])
    seasonChosen.grid(row=1,column=1)

    button = tk.Button(prevSeasonWindow, text='OK', command=ok)
    button.grid(row=2,column=0)

    prevSeasonWindow.mainloop()
这给了我

  File "C:[directory...]", line 6, in ok
    betType = betTypeVar.get()
NameError: name 'betTypeVar' is not defined

对我来说,很明显,这个错误是因为ok()没有任何参数传递给它,所以它不知道什么是“betTypeVar”,但我读过的所有教程都是这样做的,所以我遗漏了一些东西。如果我尝试实际传递ok()参数,它仍然不起作用。

代码中有两件事需要解决。首先让我们关注
createSimprevious季节性窗口

betTypeVar = tk.StringVar()
seasonVar = tk.StringVar()
您定义了两个
StringVar
,但实际上从未使用它或将它们链接到您的
组合框
对象。正确的方法是将它们设置为
textvaraible

betTypeChosen = ttk.Combobox(prevSeasonWindow, textvariable=betTypeVar, values=['Moneyline','Total'])
seasonChosen = ttk.Combobox(prevSeasonWindow, textvariable=seasonVar, values=['2018', '2017'])
接下来,
NameError:name'betTypeVar'未定义
是因为您的变量是局部变量。您正在尝试跨不同函数访问同一变量。要传递它们,您需要声明
global

def ok():
    global betTypeVar, seasonVar
    betType = betTypeVar.get()
    season = seasonVar.get()
    print(betType, season)

def CreateSimPreviousSeasonWindow():
    global betTypeVar, seasonVar
    ...
我还想指出,如果您只想检索组合框的值,那么实际上不需要创建两个
StringVar
。只要
combobox.get()
就可以了

import tkinter as tk
from tkinter import ttk
import time

def ok():
    global betTypeChosen, seasonChosen
    print (betTypeChosen.get(), seasonChosen.get())

def CreateSimPreviousSeasonWindow():
    global betTypeChosen,seasonChosen

    prevSeasonWindow = tk.Tk()

    #============= Bet Type Input =============#

    betTypeLabel = tk.Label(prevSeasonWindow, text="Bet type:").grid(row=0,column=0)
    betTypeChosen = ttk.Combobox(prevSeasonWindow,values=['Moneyline','Total'])
    betTypeChosen.grid(row=0, column=1)

    seasonLabel = tk.Label(prevSeasonWindow, text='Season:').grid(row=1, column=0)
    seasonChosen = ttk.Combobox(prevSeasonWindow, values=['2018', '2017'])
    seasonChosen.grid(row=1,column=1)

    button = tk.Button(prevSeasonWindow, text='OK', command=ok)
    button.grid(row=2,column=0)

    prevSeasonWindow.mainloop()

CreateSimPreviousSeasonWindow()

谢谢,最后一部分,与.get()一起工作。我尝试使用第一部分,它运行时没有错误,但当我按下OK按钮时,终端中没有打印任何内容。但是,当我结束程序时,在新的行(“>>>”)之前的终端中有一些空白行,因此它似乎在尝试做一些事情。尽管如此,.get()方法可能更好,所以我将尝试使它工作起来。谢谢