我怎样才能拥有启用/禁用按钮附近条目的功能?

编程入门 行业动态 更新时间:2024-10-25 12:21:33
本文介绍了我怎样才能拥有启用/禁用按钮附近条目的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

导入 tkinter 作为 tk类应用程序(tk.Frame):def __init__(self):super().__init__()self.pack()self.buttons = []self.entries = []对于范围内的 n_row(20):button = tk.Button(self, text='disable')button.grid(row=n_row, column=1)entry = tk.Entry(self)entry.grid(row=n_row, column=0, pady=(10,10))self.entries.append(entry)self.buttons.append(button)#每个按钮必须激活/停用同一行的条目如果 __name__ == '__main__':根 = tk.Tk()应用程序 = 应用程序()root.mainloop()

解决方案

问题:一个按钮,用于激活/停用同一行中的条目

定义您自己的小部件,将 tk.Buttontk.Entry 组合在自己的 tk.Frame 中.
Entry 状态会在每次点击 Button 时切换.

<小时>

相关- 使用继承扩展 tkinter 小部件.

<小时>

导入 tkinter 作为 tk类 ButtonEntry(tk.Frame):def __init__(self, parent, **kwargs):super().__init__(parent, **kwargs)self.button = tk.Button(self, text='\u2327', width=1, command=self.on_clicked)self.button.grid(row=0, column=0)self.entry = tk.Entry(self)self.columnconfigure(1, weight=1)self.entry.grid(row=0, column=1,sticky='ew')def on_clicked(self):b = 不是 self.entry['state'] == 'normal'state, text = {True: ('normal', '\u2327'), False: ('disabled', '\u221A')}.get(b)self.entry.configure(状态=状态)self.button.configure(文本=文本)

<块引用>

用法:

<预><代码>类应用程序(tk.Tk):def __init__(self):super().__init__()对于范围(5)中的行:ButtonEntry(self).grid(row=row, column=1)如果 __name__ == '__main__':应用程序().主循环()

使用 Python 测试:3.5 - 'TclVersion':8.6 'TkVersion':8.6

import tkinter as tk

class App(tk.Frame):

    def __init__(self):
        super().__init__()
        self.pack()
        self.buttons = []
        self.entries = []

        for n_row in range(20):
            button = tk.Button(self, text='disable')
            button.grid(row=n_row, column=1)
            entry = tk.Entry(self)
            entry.grid(row=n_row, column=0, pady=(10,10))
            self.entries.append(entry)
            self.buttons.append(button)
#each button must activate / deactivate the entry of the same row

if __name__ == '__main__':

    root = tk.Tk()
    app = App()
    root.mainloop()

解决方案

Question: A Button that activate / deactivate the entry in the same Row

Define your own widget which combines tk.Button and tk.Entry in a own tk.Frame.
The Entry state get toggeld on every click on the Button.


Relevant - extend a tkinter widget using inheritance.


import tkinter as tk


class ButtonEntry(tk.Frame):
    def __init__(self, parent, **kwargs):
        super().__init__(parent, **kwargs)

        self.button = tk.Button(self, text='\u2327', width=1, command=self.on_clicked)
        self.button.grid(row=0, column=0)

        self.entry = tk.Entry(self)
        self.columnconfigure(1, weight=1)
        self.entry.grid(row=0, column=1, sticky='ew')

    def on_clicked(self):
        b = not self.entry['state'] == 'normal'
        state, text = {True: ('normal', '\u2327'), False: ('disabled', '\u221A')}.get(b)
        self.entry.configure(state=state)
        self.button.configure(text=text)

Usage:


class App(tk.Tk):
    def __init__(self):
        super().__init__()

        for row in range(5):
            ButtonEntry(self).grid(row=row, column=1)


if __name__ == '__main__':
    App().mainloop()

Tested with Python: 3.5 - 'TclVersion': 8.6 'TkVersion': 8.6

这篇关于我怎样才能拥有启用/禁用按钮附近条目的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 07:49:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1391415.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:目的   按钮   功能

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!