RotaryKnob element for FreeSimpleGUI
まだ誰も着手していません。
評価
調査の方向性
Start by comparing the proposed elements/rotaryknob.py with elements/slider.py and the existing FreeSimpleGUI element integration. Confirm how element IDs and mouse events are handled, then define completion as an integrated RotaryKnob element with the requested clamped 0–255 range and usable layout controls.
索引モデルが issue の本文から書いたものです。
説明
Hello everybody, I'm Fabio from Italy. I want to honestly premise I'm not so experienced with the Python matter but I feel that FreeSimpleGUI is a great thing.
For a project of mine I need to implement a rotary knob. Many, actually.
With the prompt help of chat GPT I worked out an acceptable solution - a simple knob that starts at 3 o'clock with value 0 and produces 100 steps.
My ideal was to reach the behavior of a true potentiometer knob to produce a 8 bit control for digital potentiometers, so starting at 7 o'clock with value 0 and running clockwise up to 5 o'clock with value 255, in clamped fashion between these two points of course as a true finite potentiometer.
Strangely, something occurs with the trigonometrics and I didn't get a chance to go beyond the former working mode, so I ask your help.
Further, I tried to provide a rotaryknob.py class file in the "elements" folder, similarly to slider.py, in order to easily incorporate and manage it within the layout section of the program.
Following is the produced rotaryknob.py code. At the moment it needs to be integrated, probably in dependecies, outside the element file and thus somewhere else in the FreeSimpleGUI module, because it produces a bad ID and requires to be finalized.
Of course it would be great to incorporate at least the same controls already present on sg.slider and then, I wish, much more customization, but my knowledge ask for your help. I hope also that a simple knob can be useful to many, both approaching or also mastering both Python and FreeSG. Thank you all in advance for your precious contribution!
Fabio
import FreeSimpleGUI as sg
import math
class RotaryKnob(sg.Graph): # Inheriting from sg.Graph
def init(self, key, size=(150, 150), initial_value=0, **kwargs):
super().init(
canvas_size=size,
graph_bottom_left=(0, 0),
graph_top_right=size,
key=key,
enable_events=True,
background_color='black',
**kwargs
)
self.center = (size[0] // 2, size[1] // 2)
self.knob_radius = min(size) // 2.5
self.angle = math.pi / 2 # Initial position at hour 6 (90 degrees)
self.value = initial_value
self.is_dragging = False
self.draw_knob() # Draw the initial knob
def draw_knob(self):
self.delete_figure(self.key) # Clear previous drawings
# Draw knob base without key argument
self.draw_circle(self.center, self.knob_radius, fill_color='grey')
knob_line = self.angle_to_point(self.angle)
self.draw_line(self.center, knob_line, width=10, color='red') # Draw the knob indicator
def angle_to_point(self, angle):
x = self.center[0] + self.knob_radius * math.cos(angle)
y = self.center[1] + self.knob_radius * math.sin(angle)
return (x, y)
def point_to_angle(self, point):
dx = point[0] - self.center[0]
dy = point[1] - self.center[1]
return math.atan2(dy, dx)
def handle_drag(self, mouse_position):
if self.is_dragging:
self.angle = self.point_to_angle(mouse_position) # Use point only
self.angle = self.angle % (2 * math.pi) # Normalize angle
# Clamp the angle for allowed positions
if self.angle < math.pi: # Only allow movement CW up to hour 5
self.angle = max(self.angle, math.pi) # No more than hour 6
self.value = int((self.angle / (2 * math.pi)) * 100) # Mapping the angle to a value between 0 and 100
self.draw_knob() # Redraw knob
def set_dragging(self, dragging):
self.is_dragging = dragging # Update dragging state
Function to listen for mouse events to handle dragging
def listen_for_events(knob):
while True:
event, values = sg.read_all_windows()
if event == knob.key:
knob.handle_drag(values[knob.key])
if event in (sg.WIN_CLOSED, 'Exit'):
break
sg.Window.close()
- 主要言語
- Python
- スター
- 867
- フォーク
- 106
- 平均マージ
- 1分
- マージ済み PR(30日)
- 2
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
spyoungtech/FreeSimpleGUI のほかの issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
spyoungtech/FreeSimpleGUI#109 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
spyoungtech/FreeSimpleGUI#86 · コメント 1 件 · リアクション 2 件 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 58/100
spyoungtech/FreeSimpleGUI#101 · リアクション 8 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
spyoungtech/FreeSimpleGUI#99 · リアクション 1 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 20/100
spyoungtech/FreeSimpleGUI#95 ·
spyoungtech/FreeSimpleGUI の issue をすべて見る
似ている issue
-
agent-ready documentation needs-triage
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
-
documentation
難易度 1/5 1時間未満 初心者へのやさしさ 91/100
-
workflow-status page template still says reusable workflows are "triggered only by workflow_call:" オープン
難易度 1/5 1時間未満 初心者へのやさしさ 92/100
-
instance instance add
難易度 1/5 1時間未満 初心者へのやさしさ 72/100
searxng/searx-instances#939 · コメント 1 件 ·
-
area-deployment area-integrations triage:bot-seen
難易度 2/5 半日 初心者へのやさしさ 86/100