Create a Spinbox that can be displayed in HEX with Python GUI Tkinter.
Spinbox does not support HEX even if you use the format option in Tkinter. However, it seems that internal addition is compatible with HEX. (increment = 1 is also valid for HEX) I wrote a sample program that explicitly shows the number of bytes as follows.
HexSpinbox.py
import tkinter as tk
class HexSpinbox(tk.Spinbox):
def __init__(self, *args, **kwargs):
self.var = tk.StringVar()
self.bytenum = kwargs.pop('bytenum')
max_val = 0x1<<(self.bytenum*8)
super().__init__(*args, **kwargs, textvariable=self.var, from_=0,to=max_val,
increment=1, command=self.cange )
def set(self, val):
s = "0x{:0%dx}" % (self.bytenum*2)
self.var.set(s.format(int(val)))
def get(self):
hstr = super().get()
return int(hstr, 16)
def cange(self):
val = super().get()
self.set(val)
if __name__ == "__main__":
print("HexSpinbox")
win = tk.Tk()
hex = HexSpinbox(win, bytenum=2)
hex.set(0xAA55)
hex.pack()
win.title("HexSpinbox test")
win.mainloop()
Recommended Posts