0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

python tkinter スライダーとスピンボックスの値連携

Last updated at Posted at 2024-08-31

python tkinter スライダーとスピンボックスの値連携

spinboxの値は spinbox1.get() で取得する

scale1の値は var_scale 変数を用意しておき、
tk.scale1 = ttk.Scale で variable= のオプションで設定する

self.var_scale1 = tk.IntVar(self.root)
self.scale1 = tk.Scale(self.frame03, from_=0, to=255, orient=tk.HORIZONTAL, resolution=1, variable=self.var_scale_1, length=200, showvalue=False, command=self.func_scale_1_scrolled) # resolution=ステップ値, length=GUIフォームのサイズ:横幅 label=記述なしでラベル表示しない、showvalue=Falseで値表示させない

import tkinter as tk
from tkinter import ttk

def on_spinbox_change():
    # Spinboxの値を取得してScaleに反映
    scale1.set(float(spinbox1.get()))

def on_scale_change(val):
    # Scaleの値を取得してSpinboxに反映
    spinbox1.delete(0, tk.END)
    spinbox1.insert(0, f"{float(val):.3f}")

root = tk.Tk()
root.title("Spinbox and Scale Example")

# Spinboxを作成
spinbox1 = ttk.Spinbox(
    root, from_=0.001, to=10.000, increment=0.001, format="%.3f", command=on_spinbox_change)
spinbox1.pack(pady=10)

# Scaleを作成
scale1 = ttk.Scale(
    root, from_=0.001, to=10.000, orient="horizontal", command=on_scale_change)
scale1.pack(pady=10)

# 初期値設定
spinbox1.set(0.001)
scale1.set(0.001)

root.mainloop()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?