本文介绍了如何获取 Checkbuttons 的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Checkbuttons 是动态生成的,它们从 python 列表中获取文本.我需要一个逻辑来捕获选定的 checkbuttons 文本.根据我在任何地方的研究,他们都返回复选框的状态而不是文本.请帮忙.

Checkbuttons gets generated dynamically and they are getting text from a python list.I need a logic for capturing selected checkbuttons text .As per my research everywhere they are returning the state of checkbox instead of text.Please help.

cb_list =['pencil','pen','book','bag','watch','glasses','passport','clothes','shoes','cap']
try:
    r = 0
    cl = 1
    for op in cb_list:
        cb = Checkbutton(checkbutton_frame, text=op, relief=RIDGE)
        cb.grid(row=r, column=cl, sticky="W")
        r = r + 1
except Exception as e:
    logging.basicConfig(filename=LOG_FILENAME, level=logging.ERROR)
    logging.error(e)
    # print (e)
 selected_item = Text(self, width=30, height=20, wrap=WORD)
 selected_item.grid(row=1, column=6, padx=20, pady=20, columnspan=2, sticky=E)

 display_button = Button(self, text='DISPLAY', command=display()
 convert_button.grid(row=1, column=8, padx=20, pady=20)

推荐答案

不要覆盖相同的变量 cb,而是尝试使用可迭代类型,例如 dictionary.您还应该将 Checkbutton 的值附加到 tkinter 变量类,例如 BooleanVar,以便轻松跟踪其状态 &价值.

Instead of overwriting the same variable, cb, try using an iterable type such as dictionary. You should also need to be attaching the value of Checkbutton to a tkinter variable class, such as BooleanVar, in order to easily track its status & value.

下面的代码生成一个 GUI,每次选择 Checkbutton 时都会重写 Text.它首先填充一个字典,cbs,列表中的项目cb_list 作为键,tk.Checkbutton 对象作为值.

The code below produces a GUI that re-writes Text each time a Checkbutton is selected. It first populates a dictionary, cbs, with items from a list, cb_list, as keys and tk.Checkbutton objects as the values.

Checkbutton 对象使得每个对象都附加到一个特殊的对象,Tkinter 变量类,BooleanVar,它有一个 get 方法返回Checkbutton 它在调用时附加到当前值.在这种情况下,每个 Checkbutton 如果选中,则将 True 作为其值,如果未选中,则将 False 作为其值.

Checkbutton objects are so that each is attached to a special object, Tkinter Variable class, BooleanVar, which has a get method that returns the Checkbutton it is attached to's current value when called. In this case, each Checkbutton holds True if checked, and False if unchecked as its value.

每个Checkbutton 还附加到一个方法,update_text,当按下任何Checkbutton 时调用该方法.在该方法中,对于 cbs 中的每个 Checkbutton,它首先检查 Checkbutton 是否具有 True 值,如果是,则将其 text 附加到 _string.在对所有 Checkbuttons 完成此操作后,该方法将首先删除Text 小部件中的整个文本,然后将 _stringText.

Each Checkbutton is also attached to a method, update_text, which is called when any of the Checkbutton is pressed. In that method for every Checkbutton in cbs, it first checks if the Checkbutton has True value, if so it appends its text to a _string. After this has been done for all Checkbuttons, the method then proceeds as first deleteing the entire text in the Text widget, then it puts _string to the Text.

代码:

import tkinter as tk


def update_text():
    global cbs
    _string = ''
    for name, checkbutton in cbs.items():
        if checkbutton.var.get():
            _string += checkbutton['text'] + '\n'
    text.delete('1.0', 'end')
    text.insert('1.0', _string)


if __name__ == '__main__':
    root = tk.Tk()
    text = tk.Text(root)

    cb_list =['pencil','pen','book','bag','watch','glasses','passport',
                                                'clothes','shoes','cap']
    cbs = dict()
    for i, value in enumerate(cb_list):
        cbs[value] = tk.Checkbutton(root, text=value, onvalue=True,
                                offvalue=False, command=update_text)
        cbs[value].var = tk.BooleanVar(root, value=False)
        cbs[value]['variable'] = cbs[value].var

        cbs[value].grid(row=i, column=0)

    text.grid()
    root.mainloop()

textCheckbutton 小部件的一个选项,这意味着您可以使用 cget(widget.cget('option')) 或只是 widget['option'].


text is an option to Checkbutton widget, which means you can get its value using cget(widget.cget('option')) or simply widget['option'].

这篇关于如何获取 Checkbuttons 的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 07:09