Let's create a function to hold down Button in Tkinter

I've just started using Tkinter myself, so maybe it's easy to do No matter where I looked, I couldn't find the ability to hold down the Button and lock it. If anyone knows how to do it, can you tell me?

I couldn't find a way to do it, so I tried to implement it forcibly. It's a pretty wicked way, so it may not be helpful, but I'll list it below.

What you want to do

I want to create a GUI that edits 8-bit binaries using buttons.

problem

Can't I keep pushing with Tkinter Button? Button can be disabled. Button label (picture on the surface) can be changed But can't the relief of Button be changed?

Solution and result of making

PushButton.png

I discarded the generated button and tried to recreate everything with relief as it was pushed again.

BinEditor.py


import sys
import tkinter as tk

class BinEditFrame(tk.Frame):
    def __init__(self, master):
        super().__init__(master)
        self.val = 0x00
        self.bits = []
        for i in range(8):
            btn = tk.Button(self,text=str(i), relief='raised', command=self.callback(i))
            btn.pack(sid='right')
            self.bits.append(btn)
        print("start")

    def callback(self, i):
        def push():
            self.val ^= (1<<i)
            print(self.val)

            #All Button Delete
            for bit in self.bits:
                bit.destroy()
            self.bits.clear()

            #All Button ReCreate
            for j in range(8):
                if (self.val & (1<<j) > 0):
                    btn = tk.Button(self,text=str(j), relief='sunken',
                                    command=self.callback(j) )
                else:
                    btn = tk.Button(self,text=str(j), relief='raised',
                                    command=self.callback(j) )
                btn.pack(sid='right')
                self.bits.append(btn)
        return push
        

if __name__ == "__main__":
    print("BinEditor")
    win = tk.Tk()
    be = BinEditFrame(win)
    be.pack()
    win.mainloop()





Recommended Posts

Let's create a function to hold down Button in Tkinter
Create a function to get the contents of the database in Go
To execute a Python enumerate function in JavaScript
How to create a function object from a string
I want to create a window in Python
How to create a JSON file in Python
How to create a Rest Api in Django
How to Mock a Public function in Pytest
Try to dynamically create a Checkbutton with Python's Tkinter
I want to write in Python! (2) Let's write a test
Create a plugin to run Python Doctest in Vim (2)
Create a plugin to run Python Doctest in Vim (1)
Let's create a function for parametrized test using frame object
Covector to think in function
Create a function to display images like Jupyter / RStudio [Docker]
Create a dictionary in Python
Create a Mastodon bot with a function to automatically reply with Python
How to call a function
Let's create a script that registers with Ideone.com in Python.
A handy function to add a column anywhere in a Pandas DataFrame
Let's draw a logistic function
To return char * in a callback function using ctypes in Python
In omegaconf, let's pass the direct parameter file to the function
Create a tool to check scraping rules (robots.txt) in Python
Check the Check button in Tkinter to allow Entry to be edited
How to change the color of just the button pressed in Tkinter
How to create a heatmap with an arbitrary domain in Python
I wrote a function to load a Git extension script in Python
Attempt to extend a function in the library (add copy function to pathlib)
Create user authentication function in Airflow
Create a CSV reader in Flask
Create a python GUI using tkinter
Create a DI Container in Python
Steps to create a Django project
Implement a date setter in Tkinter
How to create a Conda package
Create a binary file in Python
How to make a recursive function
How to create a Dockerfile (basic)
Create a Kubernetes Operator in Python
5 Ways to Create a Python Chatbot
Create a random string in Python
How to create a config file
Create a LINE Bot in Django
Create a shortcut to run a Python file in VScode on your terminal
Let's create a chat function with Vue.js + AWS Lambda + dynamo DB [AWS settings]
Various methods to numerically create the inverse function of a certain function Introduction
[Python / Tkinter] Search for Pandas DataFrame → Create a simple search form to display
Create a command to delete all temporary files generated in a specific folder
How to create a wrapper that preserves the signature of the function to wrap
Let's create a customer database that automatically issues a QR code in Python
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx
What is the fastest way to create a reverse dictionary in python?
I tried to create a linebot (implementation)
How to create a clone from Github
Create a Python function decorator with Class
Create a bot to retweet coronavirus information
How to create a git clone folder
I want to print in a comprehension
I tried to create a linebot (preparation)
Precautions when pickling a function in python