簡體   English   中英

如何將 slider 中的值分配給變量? 我只能用.get()來打印output,不能存儲

[英]How can I assign the value from the slider to a variable? I can only use .get() to print the output, not store it

我需要它將 slider 的值存儲在變量中,例如單擊按鈕時的v 作為上下文,我需要這個,所以我可以有一個 slider 來設置我的撲克游戲 A 級項目的起始金額。

from tkinter import * 

root = Tk()

def show_values():
    print (w.get()) #would I put it in this function?

canvas = Canvas(root, width = 700, height = 200, bg = "blue")
w = Scale(root,from_=0, to=200,length = 200, orient=HORIZONTAL,bg = "green")
Button(root, text='Show', command=show_values).pack() 
w.pack()
canvas.pack()

mainloop()

我猜你是在 GUI 定義的主體中設置 v 。 需要在相關按鈕的命令function中設置。

from tkinter import * 

root = Tk()

v = 0 
def set_value():    
    global v
    v = w.get()  # v is set here

def do_print():
    print( v )   # v can be read here.

canvas = Canvas(root, width = 700, height = 200, bg = "blue")
w = Scale(root,from_=0, to=200,length = 200, orient=HORIZONTAL,bg = "green")
Button(root, text='Set', command=set_value).pack()     
Button(root, text='Print', command = do_print).pack() 
w.pack()
canvas.pack()

mainloop()

“設置”按鈕設置 v 的值,可以通過單擊“打印”按鈕讀取該值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM