여러 Tk 위젯은 Ttk 버전에도 존재합니다. 일반적으로 그들은 동일한 일반적인 동작을하지만 인스턴스 별 모양 속성 (예 : bg
등)보다는 "스타일"과 "테마"를 사용합니다. Ttk 위젯은 기본적으로 OS의 창 관리자의 "표준 모양"을 취하므로 외형에 대한 구성은 필요하지 않습니다. showvalue
및 tickinterval
(reference 참조)ttk.Scale을 tk.Scale처럼 동작시키는 방법?
그러나 어떤 이유로 ttk.Scale
위젯은 tk.Scale
위젯 두 가지 매우 유용한 옵션이 없습니다. 이것은 외관상보다는 행동에 대한 것이기 때문에 이상합니다.
ttk
모양을 유지하면서이 두 가지 옵션을 "적용"하는 것이 좋습니다. 다음 코드는 이에 대한 내 서투른 시도입니다. 문제는 더 좋은 방법이 있을까요? (물론 모든 것을 클래스에 캡슐화하는 것 외에) 그리고 반 자동으로 tickinterval
을 어떻게 합리적으로 얻을 것인가? (아래 코드 에서처럼 "손으로"하는 것보다).
import tkinter as tk
import tkinter.ttk as ttk
# initial setup
root = tk.Tk()
frame = tk.Frame(root)
#################################################################
# create a tk slider showing current value and ticks
# (showvalue=True is the default)
tkslider = tk.Scale(frame, from_=-4, to=4,
orient=tk.HORIZONTAL, tickinterval=2)
#################################################################
#################################################################
# create a ttk slider showing current value and ticks
# use a ttk frame to get ttk style background
ttkslider = ttk.Frame(frame)
# define a callback function to update the value label
def ttk_slider_callback(value):
# 'value' seems to be a string - bug or feature?
value_label.config(text=round(float(value)))
# 'text' can apparently be an int and gets converted into str
# (...) possibly do other stuff
# decompose frame into two ttk labels and a ttk scale
value_label = ttk.Label(ttkslider, text=0)
actual_slider = ttk.Scale(ttkslider, from_=-4, to=4,
command=ttk_slider_callback)
# (orient=tk.HORIZONTAL is the default)
ticks_label = ttk.Label(ttkslider, text=' -4 -2 0 2 4 ')
# put it all together
value_label.grid()
actual_slider.grid()
ticks_label.grid()
#################################################################
# final setup
tkslider.grid(row=0, column=0)
ttkslider.grid(row=0, column=1)
frame.grid()
root.mainloop()
이전 코드의 결과는 이전에 actualy (오른쪽 왼쪽에 Tk의 규모와 TTK 규모로, 다음과 같이 OS/윈도우 매니저에 따라 분명히 달라집니다, 비늘을 할 수있다 "슬라이딩")
몇 가지 아이디어 : 1)'showvalue' 구현을 위해서 여러분은'Scale'과'Label' 위젯에 대해 하나의'StringVar'를 사용할 수 있습니다. 불행히도'ttk.Scale'은 값의 문자열 표현을 제어하는'digits' 옵션이 없습니다.어쨌든, 제 의견으로는 지금 당장은 괜찮습니다. 2)'tickinterval' 구현은 좀 더 까다 롭습니다. 원하는 tickoidval 값을 알기 때문에 모든 "tick"값을 생성 할 수 있습니다. 레이블과 [coords] (https://www.tcl.tk/man/tcl/ TkCmd/ttk_scale.htm # M23) ('tk'는'root.tk.call (actual_slider, 'coords', str (round (float (value))))')')를 호출하고이 레이블을'Scale' . – CommonSense
@CommonSense 1) 게시하기 전에 인터 위젯 통신에'StringVar'를 실제로 사용하려고 시도했지만 슬프게도 실제로는 레이블 위치를 엉망으로 만드는 부동 소수점이있는 부동 소수점이 표시됩니다. 따라서 정확한 숫자를 얻기 위해 숫자를 "수동으로"반올림합니다. 2) 링크를 주셔서 감사합니다. – Dalker