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.
: 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