如何在 Tkinter 中使用刻度表改變列表項值?


Tkinter Entry 小工具是一個輸入小工具,僅支援單行使用者輸入。它接受文字欄位中的所有字元,除非或直到未設定輸入限制。我們可以藉助 Scale 小工具來更改 Entry 小工具的值。Scale 小工具包含一個較低值和一個閾值,後者限制了使用者在特定範圍內調整值。

要在更新 Scale 小工具的值時更新 Entry 小工具中的值,我們必須建立一個變數,該變數必須同時提供給刻度和條目小工具。

示例

#Import the Tkinter Library
from tkinter import *
from tkinter import ttk

#Create an instance of Tkinter Frame
win = Tk()

#Set the geometry of window
win.geometry("700x350")

#Create an Integer Variable to set the initial value of Scale
var = IntVar(value=10)

#Create an Entry widget
entry = ttk.Entry(win,width= 45,textvariable=var)
scale = Scale(win, from_=10, to=200, width= 20, orient="horizontal", variable=var)

entry.place(relx= .5, rely= .5, anchor= CENTER)
scale.place(relx= .5, rely= .6, anchor = CENTER)

win.mainloop()

輸出

執行上述程式碼將顯示一個 Entry 小工具和一個 Scale,可用於更新 Entry 小工具中的值。

更新於: 2021 年 5 月 26 日

1 千次以上瀏覽

開啟你的 職業

完成課程即可獲得認證

開始
廣告
© . All rights reserved.