Python 从条目、组合框、文本中获取值的通用函数

Python 从条目、组合框、文本中获取值的通用函数,python,tkinter,tkinter-entry,Python,Tkinter,Tkinter Entry,是否可以使用通用函数从条目中获取值 大概是这样的: def returnInput(obj): _x = StringVar() obj.configure(textvariable=_x) return str(_x.get()) 感谢对大多数tkinter文本函数的帮助,var=obj.get()最常用,只有少数例外 例如: entry.get() listbox.get(listbox.curselection()) 或导出组合框的选择 使用这些方法比创建函数要

是否可以使用通用函数从条目中获取值

大概是这样的:

def returnInput(obj):
    _x = StringVar()
    obj.configure(textvariable=_x)
    return str(_x.get())

感谢对大多数tkinter文本函数的帮助,var=obj.get()最常用,只有少数例外

例如:

entry.get()
listbox.get(listbox.curselection())
或导出组合框的选择


使用这些方法比创建函数要简单得多。

对于大多数tkinter文本函数,var=obj.get()最常用,只有少数例外

例如:

entry.get()
listbox.get(listbox.curselection())
或导出组合框的选择


使用这些方法比创建函数要容易得多。

不,不是那样的。但是,您可以定义如下所示的函数:

def uni_get(widget):

    wgt_typ = type(widget).__name__
    if wgt_typ == 'Label' or wgt_typ == 'Button':
        disp_str = widget['text']

    elif wgt_typ == 'Text':
        disp_str = widget.get('1.0', 'end-1c')

    elif wgt_typ == 'Combobox' or wgt_typ == 'Entry':
        disp_str = widget.get()

    return disp_str

演示示例:

import tkinter as tk
from tkinter import ttk

def uni_get():
    #to dynamically update the selected widget passed to uni_get
    global cbb
    widget = root.winfo_children()[cbb.current()]

    wgt_typ = type(widget).__name__
    if wgt_typ == 'Label' or wgt_typ == 'Button':
        disp_str = widget['text']

    elif wgt_typ == 'Text':
        disp_str = widget.get('1.0', 'end-1c')

    elif wgt_typ == 'Combobox' or wgt_typ == 'Entry':
        disp_str = widget.get()

    print(disp_str)

root = tk.Tk()

cbb = ttk.Combobox(root)
ent = tk.Entry(root)
txt = tk.Text(root)
lbl = tk.Label(root)
btn = tk.Button(root, command=uni_get)

###     default widget configs      ###
cbb['values'] = ["Combobox", "Entry", "Text", "Label", "Button"]
cbb.current(0)
ent.insert('0', "Entry")
txt.insert('1.0', "Text")
lbl['text'] = "Label"
btn['text'] = "Button"

###     layout      ###
cbb.pack()
ent.pack()
txt.pack()
lbl.pack()
btn.pack()

root.mainloop()

不,不是那样的。但是,您可以定义如下所示的函数:

def uni_get(widget):

    wgt_typ = type(widget).__name__
    if wgt_typ == 'Label' or wgt_typ == 'Button':
        disp_str = widget['text']

    elif wgt_typ == 'Text':
        disp_str = widget.get('1.0', 'end-1c')

    elif wgt_typ == 'Combobox' or wgt_typ == 'Entry':
        disp_str = widget.get()

    return disp_str

演示示例:

import tkinter as tk
from tkinter import ttk

def uni_get():
    #to dynamically update the selected widget passed to uni_get
    global cbb
    widget = root.winfo_children()[cbb.current()]

    wgt_typ = type(widget).__name__
    if wgt_typ == 'Label' or wgt_typ == 'Button':
        disp_str = widget['text']

    elif wgt_typ == 'Text':
        disp_str = widget.get('1.0', 'end-1c')

    elif wgt_typ == 'Combobox' or wgt_typ == 'Entry':
        disp_str = widget.get()

    print(disp_str)

root = tk.Tk()

cbb = ttk.Combobox(root)
ent = tk.Entry(root)
txt = tk.Text(root)
lbl = tk.Label(root)
btn = tk.Button(root, command=uni_get)

###     default widget configs      ###
cbb['values'] = ["Combobox", "Entry", "Text", "Label", "Button"]
cbb.current(0)
ent.insert('0', "Entry")
txt.insert('1.0', "Text")
lbl['text'] = "Label"
btn['text'] = "Button"

###     layout      ###
cbb.pack()
ent.pack()
txt.pack()
lbl.pack()
btn.pack()

root.mainloop()

您编写的代码毫无意义-在有人将任何内容放入对象之前,它从
StringVar
获取数据。您必须在开始时将
StringVar
分配给
Entry
,但您在不同的时刻从
StringVar
中获取值。您的代码毫无意义-在有人将任何内容放入对象之前,它从
StringVar
中获取数据。您必须在开始时将
StringVar
分配给
Entry
,但您在不同时刻从
StringVar
获得值。您的第一个语句为false
obj.get()。其他使用
get()
的小部件需要一个或多个参数。这正好证明了我关于在使用变量赋值时使用函数更容易的观点。您的第一个语句是错误的
obj.get()。使用
get()
的其他小部件需要一个或多个参数。这正好证明了我关于在使用变量赋值时使用函数的观点可能更简单。我得到了以下错误:
Tkinter回调回溯中的异常(最近一次调用):文件“C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\Tkinter\\uuuu init\uuuuuuuuuuuuuuy”,第1699行,在uni\u-get(X)文件“C:\Users\User\Desktop\PyEngine\gui.py”的保存x1=uni\u-get(X)文件“C:\Users\User\Desktop\PyEngine\gui.py”的第70行,uni\u-get打印(disp\u-str)中,返回self.func(*args)文件“C:\Users\Desktop\PyEngine\gui.py”中UnboundLocalError:赋值前引用的局部变量“disp_str”
修改函数时一定出错了。尝试在函数中添加
global disp_str
作为第一行。发现错误时,它位于另一个函数中。现在它工作了,非常感谢@D.W没问题。我得到了这个错误:
Tkinter回调回溯异常(最后一次调用):文件“C:\Users\User\AppData\Local\Programs\Python36-32\lib\Tkinter\\ uu init\uuuuuuuuuuuuu.py”,第1699行,在uuuuuuu call\uuuuuuuuuu返回self.func(*args)文件“C:\Users\User\Desktop\PyEngine\gui.py”,第75行,在save x1=uni get(X)文件中“C:\Users\User\Desktop\PyEngine\gui.py”,uni_-get-print(disp_-str)中的第70行UnboundLocalError:赋值前引用的局部变量'disp_str'
修改函数时一定出错了。请尝试在函数中添加
global disp_str
,作为第一行。发现错误时,它在另一个函数中。现在可以工作了,非常感谢!@D.W没问题。