我正在尝试使用模拟量表来编写用Python 3.6编写的tkinter / ttk GUI。
我想显示伏特和psi值。
到目前为止,我的研究引导我this(linuxcnc / pyvcp?),this(perl / tk)和this(tk)。
我是否有可能整合这三个或者我应该尝试使用tkinter.Canvas
来实现自己的衡量标准?
答案 0 :(得分:2)
我创建了一个名为tk_tools
的包,其中包含一个这样的模拟量表。
pip install tk_tools
来源:https://github.com/slightlynybbled/tk_tools
文档:https://tk-tools.readthedocs.io/en/latest/docs/widgets/#rotaryscale
您正在寻找RotaryScale
小部件。可以在examples/rotary_scale.py
中找到使用示例。简短版本:
import tkinter as tk
import tk_tools
root = tk.Tk()
p = tk_tools.RotaryScale(root, max_value=100.0, unit='psi')
p.grid()
p.set_value(32.7)
root.mainloop()
答案 1 :(得分:2)
我在python中翻译了第3个:
import tkinter as tk
from math import pi, cos, sin
class Meter(tk.Frame):
def __init__(self, master=None, **kw):
tk.Frame.__init__(self, master, **kw)
self.meter = []
self.angle = []
self.var = tk.IntVar(self, 0)
self.canvas = tk.Canvas(self, width=200, height=110,
borderwidth=2, relief='sunken',
bg='white')
self.scale = tk.Scale(self, orient='horizontal', from_=0, to=100, variable=self.var)
for j, i in enumerate(range(0, 100, 5)):
self.meter.append(self.canvas.create_line(100, 100, 10, 100,
fill='grey%i' % i,
width=3,
arrow='last'))
self.angle.append(0)
self.canvas.lower(self.meter[j])
self.updateMeterLine(0.2, j)
self.canvas.create_arc(10, 10, 190, 190, extent=108, start=36,
style='arc', outline='red')
self.canvas.pack(fill='both')
self.scale.pack()
self.var.trace_add('write', self.updateMeter) # if this line raises an error, change it to the old way of adding a trace: self.var.trace('w', self.updateMeter)
self.updateMeterTimer()
def updateMeterLine(self, a, l=0):
"""Draw a meter line (and recurse for lighter ones...)"""
oldangle = self.angle[l]
self.angle[l] = a
x = 100 - 90 * cos(a * pi)
y = 100 - 90 * sin(a * pi)
self.canvas.coords(self.meter[l], 100, 100, x, y)
l += 1
if l < len(self.meter):
self.updateMeterLine(oldangle, l)
def updateMeter(self, name1, name2, op):
"""Convert variable to angle on trace"""
mini = self.scale.cget('from')
maxi = self.scale.cget('to')
pos = (self.var.get() - mini) / (maxi - mini)
self.updateMeterLine(pos * 0.6 + 0.2)
def updateMeterTimer(self):
"""Fade over time"""
self.var.set(self.var.get())
self.after(20, self.updateMeterTimer)
if __name__ == '__main__':
root = tk.Tk()
meter = Meter(root)
meter.pack()
root.mainloop()