如何获取tkinter Spinbox的值?

huangapple go评论108阅读模式
英文:

How to get tkinter Spinbox's value?

问题

我正在尝试在Python Tkinter上创建一个计时器。为了设置计时器,我正在使用微调框(spinboxes)。但是,我在将我的微调框的值转换为变量time_htime_mtime_s时遇到了问题。

我尝试使用.get(),但它不起作用。当我尝试打印这些变量时,我得到了NameError: name 'spin_h' is not defined

  1. from tkinter import *
  2. window = Tk()
  3. window.title("Timer")
  4. window.geometry('350x200')
  5. hour = 0
  6. minute = 0
  7. second = 0
  8. timer = (str(hour) + ':' + str(minute) + ':' + str(second))
  9. lbl = Label(window, text=timer, font=("Arial Bold", 50))
  10. hour_s = 0
  11. min_s = 0
  12. sec_s = 0
  13. def save_time():
  14. time_h = spin_h.get()
  15. time_m = spin_m.get()
  16. time_s = spin_s.get()
  17. def new_window():
  18. set_time = Tk()
  19. spin_h = Spinbox(set_time, from_=0, to=10, width=5)
  20. spin_h.grid(column=1,row=0)
  21. spin_m = Spinbox(set_time, from_=0, to=60, width=5)
  22. spin_m.grid(column=3,row=0)
  23. spin_s = Spinbox(set_time, from_=0, to=60, width=5)
  24. spin_s.grid(column=5,row=0)
  25. h_label = Label(set_time, text='h', font=("Arial Bold", 10))
  26. h_label.grid(column=2, row=0)
  27. m_label = Label(set_time, text='m', font=("Arial Bold", 10))
  28. m_label.grid(column=4, row=0)
  29. s_label = Label(set_time, text='s', font=("Arial Bold", 10))
  30. s_label.grid(column=6, row=0)
  31. set_button = Button(set_time, text="Set Time", command=save_time)
  32. set_button.grid(column=3, row=2)
  33. btn = Button(window, text="Set Time", command=new_window)
  34. btn.grid(column=3, row=2)
  35. lbl.grid(column=3, row=0)
  36. window.mainloop()
英文:

I am trying to make a timer on python Tkinter. To set the timer, I am using spinboxes. But, I am having trouble getting the value of my spinboxes to be turned into the variables time_h, time_m and time_s.

I have tried .get() but it is not working. When I tried printing the variables I got NameError: name 'spin_h' is not defined.

  1. from tkinter import *
  2. window = Tk()
  3. window.title("Timer")
  4. window.geometry('350x200')
  5. hour = 0
  6. minute = 0
  7. second = 0
  8. timer = (str(hour) + ':' + str(minute) + ':' + str(second))
  9. lbl = Label(window, text=timer, font=("Arial Bold", 50))
  10. hour_s = 0
  11. min_s = 0
  12. sec_s = 0
  13. def save_time():
  14. time_h = spin_h.get()
  15. time_m = spin_m.get()
  16. time_s = spin_s.get()
  17. def new_window():
  18. set_time = Tk()
  19. spin_h = Spinbox(set_time, from_=0, to=10, width=5)
  20. spin_h.grid(column=1,row=0)
  21. spin_m = Spinbox(set_time, from_=0, to=60, width=5)
  22. spin_m.grid(column=3,row=0)
  23. spin_s = Spinbox(set_time, from_=0, to=60, width=5)
  24. spin_s.grid(column=5,row=0)
  25. h_label = Label(set_time, text='h', font=("Arial Bold", 10))
  26. h_label.grid(column=2, row=0)
  27. m_label = Label(set_time, text='m', font=("Arial Bold", 10))
  28. m_label.grid(column=4, row=0)
  29. s_label = Label(set_time, text='s', font=("Arial Bold", 10))
  30. s_label.grid(column=6, row=0)
  31. set_button = Button(set_time, text="Set Time", command=save_time)
  32. set_button.grid(column=3, row=2)
  33. btn = Button(window, text="Set Time", command=new_window)
  34. btn.grid(column=3, row=2)
  35. lbl.grid(column=3, row=0)
  36. window.mainloop()

答案1

得分: 0

spin_hnew_window()函数内部的局部变量,不能被save_time()函数访问。您可以在new_window()的开头将其声明为全局变量来解决此问题。- #Martineau(将其从评论改成了答案)。

谢谢Martineau。

英文:

spin_h is a variable local to the new_window() function and there cannot be accessed by the save_time() function. You could declare it a global variable at the beginning of new_window() to fix that. - #Martineau (just made it into an answer instead of a comment).

Thanks Martineau

huangapple
  • 本文由 发表于 2020年1月7日 02:10:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定