问题描述
限时送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.Button
和 tk.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:关键词]
发布评论