how can i have the function to enable / disable the entries near the buttons?

后端 未结 3 1365
小鲜肉
小鲜肉 2021-01-27 20:59
import tkinter as tk

class App(tk.Frame):

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

          


        
3条回答
  •  礼貌的吻别
    2021-01-27 21:10

    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

提交回复
热议问题