[PYTHON] Make a Spinbox that can be displayed in Binary with Tkinter

Continuation of the article posted earlier https://qiita.com/Nomisugi/items/cb2fa4f26cdf0a7888c5

Let's write the code to make Binary compatible with Spinbox of Python Tkinter. Tkinter's Spinbox does not support Binary internally, and returns the addition result as 0 or 1. I tried to write the code using that property. This sample code is an 8-bit fixed code. BIN.png

: Binspinbox.py
import tkinter as tk

class BinSpinbox(tk.Spinbox):
    def __init__(self, *args, **kwargs):
        self.var = tk.StringVar()
        super().__init__(*args, **kwargs, textvariable=self.var, from_=0,to=0xff,
                         increment=1, command=self.cange )
        self.val = 0

    def set(self, val):
        self.val = val
        self.var.set("0b{:08b}".format(int(val)))

    def get(self):
        hstr = self.var().get()
        return int(hstr, 2)

    def cange(self):
        val = super().get()
        print(val)
        if(val == '1'):
            self.val = self.val+1
            if(self.val > 0xff):
                self.val = 0x00
        else:
            if(self.val == 0x00 ):
                self.val = 0xff
            else:
                self.val = self.val-1
        self.set(self.val)


if __name__ == "__main__":
    print("BinSpinbox")
    win = tk.Tk()
    hex = BinSpinbox(win)
    hex.set(0x55)
    hex.pack()

    win.title("BinSpinbox test")
    win.mainloop()


Recommended Posts

Make a Spinbox that can be displayed in Binary with Tkinter
Make a Spinbox that can be displayed in HEX with Tkinter
Let's make a diagram that can be clicked with IPython
[Python] Make a graph that can be moved around with Plotly
Make a currency chart that can be moved around with Plotly (2)
Make a currency chart that can be moved around with Plotly (1)
Color list that can be set with tkinter (memorial)
I made a familiar function that can be used in statistics with Python
A timer (ticker) that can be used in the field (can be used anywhere)
I made a shuffle that can be reset (reverted) with Python
How to make a rock-paper-scissors bot that can be easily moved (commentary)
Create a web app that can be easily visualized with Plotly Dash
[Can be done in 10 minutes] Create a local website quickly with Django
Draw a graph that can be moved around with HoloViews and Bokeh
A story that heroku that can be done in 5 minutes actually took 3 days
Get a list of camera parameters that can be set with cv2.VideoCapture and make it a dictionary type
File types that can be used with Go
Functions that can be used in for statements
List packages that can be updated with pip
Iterative (recursive) processing with tkinter (displayed in order)
A memo for making a figure that can be posted to a journal with matplotlib
I want to create a priority queue that can be updated in Python (2.7)
I registered PyQCheck, a library that can perform QuickCheck with Python, in PyPI.
A personal memo of Pandas related operations that can be used in practice
Format DataFrame data with Pytorch into a form that can be trained with NN
Run the output code with tkinter, saying "A, pretending to be B" in python
Convert images from FlyCapture SDK to a form that can be used with openCV
Basic algorithms that can be used in competition pros
Japanese can be used with Python in Docker environment
Make a simple Slackbot with interactive button in python
Python knowledge notes that can be used with AtCoder
ANTs image registration that can be used in 5 minutes
Create a flag in settings that will be True only when testing with Django
I tried to make a memo app that can be pomodoro, but a reflection record
Morphological analysis and tfidf (with test code) that can be done in about 1 minute
Let's make a Mac app with Tkinter and py2app
The story that sendmail that can be executed in the terminal did not work with cron
A mechanism to call a Ruby method from Python that can be done in 200 lines
Limits that can be analyzed at once with MeCab
How to set up a simple SMTP server that can be tested locally in Python
A memorandum (masOS) that import tkinter could not be done with python installed from pyenv
Format summary of formats that can be serialized with gensim
It seems that Skeleton Tracking can be done with RealSense
I made a simple typing game with tkinter in Python
Make a tky2jgd plugin with no practicality in QGIS Part 2
NumPy zeros can be defined even with a size of 0
Solution when the image cannot be displayed with tkinter [python]
Let's create a script that registers with Ideone.com in Python.
Create a life game that is manually updated with tkinter
Make a tky2jgd plugin with no practicality in QGIS Part 1
Goroutine (parallel control) that can be used in the field
Text analysis that can be done in 5 minutes [Word Cloud]
Goroutine that can be used in the field (errgroup.Group edition)
I made a puzzle game (like) with Tkinter in Python
Create a web API that can deliver images with Django
I tried to make a stopwatch using tkinter in python
Scripts that can be used when using bottle in Python
Implement a thread that can be paused by exploiting yield
I investigated the pretreatment that can be done with PyCaret
I made a plug-in that can "Daruma-san fell" with Minecraft
Evaluation index that can be specified in GridSearchCV of sklearn