【问题标题】:Tkinter Spinbox using get value in command function [duplicate]Tkinter Spinbox 在命令函数中使用获取值 [重复]
【发布时间】:2017-02-06 04:31:18
【问题描述】:

我正在尝试编写一些代码,用目录中的文件名(.png 图像)填充旋转框。然后在旋转框中选择一项显示图像。我无法使用 get 从 spinbox 传递值

产生的错误是:

photo = PhotoImage(file=XS_Spinbox.get())
AttributeError: 'NoneType' object has no attribute 'get'

注意硬编码文件名有效...

photo = PhotoImage(file='Arm_DEM_192_XSplot.png')

.

XS_Spinbox = Spinbox(myframe2, values = ([x for x in os.listdir(os.curdir) if os.path.splitext(x)[1] in ('.png')]), command=update_photoImage).pack()

def update_photoImage():
#print XS_spinBox.get()
#photo = PhotoImage(file=XS_spinBox.get())
photo = PhotoImage(file=XS_Spinbox.get())
label.image = photo
return

【问题讨论】:

  • 我认为这个问题更多是关于 Tkinter 小部件 spinbox 如何将值传递给命令函数?
  • def update_photoImage(): print XS_Spinbox.get() photo = PhotoImage(file = XS_Spinbox.get()) photo = photo.subsample(2,2) #label = Label(myframe2, image= photo) label.configure(image = photo) label.image = photo print 'Image updated ...' return 这成功使用 spinbox.get data 更新图像

标签: python tkinter get


【解决方案1】:

pack() 返回None 并将其分配给XS_Spinbox

你必须分两步做:

 XS_Spinbox = Spinbox(...)

 XS_Spinbox.pack() 

【讨论】:

  • 我现在已将其调整为两步分配,但尽管 Spinbox 数据正在更新,但图像并未在函数 def update_photoImage() 中更新: print XS_Spinbox.get() photo = PhotoImage(file = XS_Spinbox.get()) photo = photo.subsample(2,2) label = Label(myframe2, image=photo) label.image = photo return 只想知道label里的图片是怎么更新的????
  • 你在控制台运行过吗?也许你会收到错误信息。您可能必须在文件名中添加 os.curdir 才能获得完整路径。
  • 你必须这样做label["image"] = photolabel.config(image=photo)
猜你喜欢
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
  • 2012-10-17
  • 2021-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多