This is a Gitlab for NZOSS Members

Commit 2d023525 authored by Eliot Blennerhassett's avatar Eliot Blennerhassett
Browse files

Drop python2 support

parent 22060798
Pipeline #8 failed with stages
in 0 seconds
# python3 only now
A virtual XY joystick using tkinter
Can be manipulated with the mouse, or set to a given XY position
import tkinter as tk
class Joystick(tk.Frame):
def __init__(self, root, label="Joystick"):
"""Joystick widget
def __init__(self, root, label=None, canvas_size=100):
tk.Frame.__init__(self, root)
self.xvar = tk.DoubleVar()
self.yvar = tk.DoubleVar()
# tk.Label(self, text=label).grid(row=0, columnspan=2)
if label is not None:
tk.Label(self, text=label).grid(row=0, columnspan=2)
cs = 100
cs = canvas_size
self.canvas = tk.Canvas(self, width=cs, height=cs, background="bisque")
self.canvas.configure(scrollregion=(0, 0, cs * 2, cs * 2))
self.canvas.grid(row=1, column=0, sticky="nsew", columnspan=2)
......@@ -37,13 +42,15 @@ class Joystick(tk.Frame):
# This is what enables scrolling with the mouse:
self.canvas.bind("<ButtonPress-1>", self.scroll_start)
self.canvas.bind("<B1-Motion>", self.scroll_move)
self.canvas.bind("<ButtonPress-1>", self._scroll_start)
self.canvas.bind("<B1-Motion>", self._scroll_move)
def get(self):
return self.xvar.get(), self.yvar.get()
def set(self, pos):
"""Set handle to pos=(x, y) where x and y range from -1.0 to 1.0
x, y = pos
......@@ -52,10 +59,10 @@ class Joystick(tk.Frame):
self.canvas.xview_moveto((1 - x) / 4.0)
self.canvas.yview_moveto((y + 1) / 4.0)
def scroll_start(self, event):
def _scroll_start(self, event):
self.canvas.scan_mark(event.x, event.y)
def scroll_move(self, event):
def _scroll_move(self, event):
self.canvas.scan_dragto(event.x, event.y, gain=1)
# transform viewport into joystick position with positive x,y in top R
# range = -1..+1
......@@ -70,7 +77,7 @@ class Joystick(tk.Frame):
if __name__ == "__main__":
root = tk.Tk()
j = Joystick(root)
j = Joystick(root, label="Joystick Demo", canvas_size=150)
j.pack(fill="both", expand=True)
j.set((0.1, -0.2))
"""Class for logging to a Tkinter Text or ScrolledText widget"""
"""Class for logging to a Tkinter Text or ScrolledText widget
import logging
st = ScrolledText(self, state="normal", font=("Courier", fs))
st.insert(END, "Log messages appear here\n")
text_handler = TextHandler(st)
import Tkinter as tk
except ImportError:
import tkinter as tk
import logging
import tkinter as tk
class TextHandler(logging.Handler):
"""A vane or meter widget in tkinter
# python3 only...
from math import degrees, radians
from cmath import phase, rect
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment