简体   繁体   English

Python-我可以使用Lambda函数通过tkinter生成重复的GUI吗? (标签/文本/按钮)

[英]Python - Can I use a lambda function to generate a repetitive GUI with tkinter? (labels/text/buttons)

I have a very repetitive GUI to implement - think tens of label/text/button fields, for a data entry form. 我有一个非常重复的GUI来实现-为数据输入表单考虑数十个标签/文本/按钮字段。 The sizes of each repeated section can be the same - in fact, everything can be the same except the text in the label and the variable that the data from the text field is assigned to upon completion. 每个重复部分的大小可以相同-实际上,除了标签中的文本和完成时将文本字段中的数据分配给变量的变量之外,所有内容都可以相同。

I've worked with an engineer who used lambda functions to generate sub-functions in a very complex way which I almost followed, but not quite 100%. 我曾与一名工程师合作,该工程师使用lambda函数以非常复杂的方式生成子函数,我几乎遵循,但并不是100%。 I was hoping, since this is a similar, mostly repetitive task, that there was some way to use a formulaic function to repeat the GUI creation work for me, rather than to have to type out each and every GUI item. 我希望,因为这是一个类似的,通常是重复性的任务,所以希望有某种方法可以使用公式化函数为我重复GUI创建工作,而不必键入每个GUI项目。

Is it possible to have repetitive GUI elements generated by a function, and if so, is that a lambda function? 是否可以由一个函数生成重复的GUI元素,如果是的话,那是lambda函数吗? Or is there a different (or better) way to accomplish the same "not repeating myself"? 还是有其他(或更好)的方法来实现相同的“不重复自己”?

Is it possible to have repetitive GUI elements generated by a function, and if so, is that a lambda function? 是否可以由一个函数生成重复的GUI元素,如果是的话,那是lambda函数吗?

Yes, it's possible to create gui elements with a function, and no, it's not a lambda function. 是的,可以使用函数创建gui元素,不,它不是lambda函数。 Not only is it possible, it's arguably a best practice to create gui elements in a function, though you could also just use a simple loop or a conventional function. 在函数中创建gui元素不仅是可能的,而且可以说是最佳实践,尽管您也可以只使用简单的循环或常规函数。

When creating groups of widgets that are somehow tied together, it's even better to create a custom class that can encapsulate all of the behavior and provide a simple interface for the rest of the program. 创建以某种方式捆绑在一起的小部件组时,最好创建一个自定义类,该类可以封装所有行为并为程序的其余部分提供简单的接口。

Example

In the following example, we want to create a series of widgets with a label, an entry, and a submit button. 在下面的示例中,我们要创建一系列带有标签,条目和提交按钮的小部件。 It is going to be implemented as a class, since we are in effect creating an object that represents one form field. 由于我们实际上是在创建一个表示一个表单字段的对象,因此它将作为一个类来实现。

Note: the following code assumes you imported tkinter with import tkinter as tk . 注意:以下代码假定您导入tkinter,而import tkinter as tk

First, lets create a callback that sets a value in a dictionary, and also prints out the value for debugging purposes. 首先,让我们创建一个回调,该回调在字典中设置一个值,并打印出该值以用于调试。 The callback will be given the name of the field, and the value that the user entered: 回调将被赋予字段名称和用户输入的值:

data = {}
def handle_submit(name, value):
    print("you submitted '%s' for %s" % (value, name))
    data[name] = value

Next, the code to create 10 items would might look like this: 接下来,创建10个项目的代码可能如下所示:

root = tk.Tk()
for i in range(1, 11):
    field_name = "field %s" % i
    row = CustomWidget(root, name=field_name, callback=handle_submit)

Finally, we need to create our class. 最后,我们需要创建我们的类。 We inherit from tk.Frame so that we can use it like any other tkinter widget. 我们继承自tk.Frame因此我们可以像使用任何其他tkinter小部件一样使用它。 It needs to take parameters for its parent, the field name, and the function to call when the user presses submit: 它需要为其父项,字段名和用户按下submit时要调用的函数获取参数:

class CustomWidget(tk.Frame):
    def __init__(self, parent, name, callback):
        tk.Frame.__init__(self, parent)

        self.name = name
        label_text = name.title() + ":"
        self.callback = callback
        self.label = tk.Label(self, text=label_text, anchor="e")
        self.entry = tk.Entry(self)
        self.button = tk.Button(self, text="Submit", command=self.submit)

        self.button.pack(side="right")
        self.label.pack(side="left")
        self.entry.pack(side="left", fill="x", expand=True)

    def submit(self):
        self.callback(self.name, self.entry.get())

暂无
暂无

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

相关问题 我如何格式化 Python Tkinter 中的标签和按钮,例如更改文本的颜色、字体和大小以及按钮的背景? - How can I format labels and buttons in Python Tkinter, for example changing the colour, font and size of the text and the background for buttons? 当我在Tkinter中使用文本小部件时,按钮从GUI表单中消失 - Buttons disappear form the GUI Form when i use Text Widget in Tkinter 用于替换标签的Python Tkinter按钮 - Python Tkinter buttons to replace labels 如何使用tkinter放置按钮或文本框或标签? - How do I position buttons or text boxes or labels using tkinter? 通过lambda函数和tkinter按钮发送信息会导致TypeError [Python] - Sending information via a lambda function and tkinter buttons results in TypeError [Python] Python Tkinter 按钮 - 无文本 - Python Tkinter buttons - no text 如何使用 tkinter 创建带有 lambda 的 0-9 数字按钮? - How can I create 0-9 number buttons with lambda using tkinter? 我目前正在使用 python 制作一个用于 GUI 的 tkinter 计算器。 我已经创建了所有必要的按钮和文本框 - im currently making a calculator using python an tkinter for the GUI. I have created all the buttons and a text box necessary 如何将 python eval() 函数用于 tkinter 文本窗口? - How do I use the python eval() function for a tkinter text window? 将函数结果传递给 tkinter GUI 文本小部件 python3 - Pass function result to tkinter GUI Text widget python3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM