【发布时间】:2020-08-27 07:44:45
【问题描述】:
当我点击“apply_but”按钮时,我希望更新此按钮上的图片。但它不起作用。 另一个问题是,当我打开这个窗口时——“噪声插入”也可以使用“曲线”方法并打开另一个带有标签“你在找什么?”的窗口。但是这种方法只有在点击“img_but”按钮后才有效。
class Noise(Toplevel):
def __init__(self, pic):
super().__init__()
w = self.winfo_screenwidth() // 2 - 300 # ширина экрана
h = self.winfo_screenheight() // 2 - 300 # высота экрана
self.geometry('600x600+{}+{}'.format(w, h))
self.title('Noise insertion')
self.resizable(width=False, height=False)
self['bg'] = '#7be8cf'
Label(self, text='Noise insertion', font=('Comic Sans MS', 20),
fg='#3d3d42', bg='#7be8cf').place(x=200, y=0)
var = IntVar()
var.set(0)
var2 = IntVar()
var2.set(0)
self.yes_but = Radiobutton(self, text="Yes", variable=var, value=0,
font=('Comic Sans MS', 20),
width='10', height='1', activebackground='#e6f547',
bg='#7be8cf')
self.no_but = Radiobutton(self, text="No", variable=var, value=1,
font=('Comic Sans MS', 20),
width='10', height='1', activebackground='#e6f547',
bg='#7be8cf')
self.salt_but = Radiobutton(self, text="salt and peper", variable=var2, value=0,
font=('Comic Sans MS', 20),
width='10', height='1', activebackground='#e6f547',
bg='#7be8cf')
self.gauss_but = Radiobutton(self, text="gauss noise", variable=var2, value=1,
font=('Comic Sans MS', 20),
width='10', height='1', activebackground='#e6f547',
bg='#7be8cf')
self.scale = Scale(self, orient=VERTICAL, from_=0, to=100, bg='#7be8cf',
activebackground='#e6f547', font='10',
highlightbackground='#7be8cf', label='%', fg='#f53be2')
self.apply_but = Button(self, text='Apply', font=('Comic Sans MS', 10),
width='15', bg='grey', fg='white', bd=20, command=self.update_pic(pic))
self.img_but = Button(self, image=pic, command = self.curves())
# Pack()
self.yes_but.place(x=0, y=100)
self.no_but.place(x=0, y=50)
self.salt_but.place(x=220, y=50)
self.gauss_but.place(x=220, y=100)
self.scale.place(x=500, y=50)
self.apply_but.place(x=220, y=200)
self.img_but.place(x=150, y=300)
def update_pic(self, pic):
var = self.scale.get()
scale_res = self.scale.get()
self.img = ImageTk.PhotoImage(get_pic('color_dragon.jpg', size=
(300,200)).filter(ImageFilter.GaussianBlur))
#self.img_but[image] = self.img ???
pass
def curves(self):
Curves()
pass
class Curves(Toplevel):
def __init__(self):
super().__init__()
w = self.winfo_screenwidth() // 2 - 300 # ширина экрана
h = self.winfo_screenheight() // 2 - 300 # высота экрана
self.geometry('600x600+{}+{}'.format(w, h))
self.title('Curves')
self.resizable(width=False, height=False)
self['bg'] = '#7be8cf'
Label(self, text='What are u looking for?', font=('Comic Sans MS', 20),
fg='#3d3d42', bg='#7be8cf').pack()
【问题讨论】:
-
command = self.curves()应该是command = self.curves也可以在update_pic()self.img_but.image = self.img中说 -
@CoolCloud 如果我说
self.img_but.image = self.img,那么我会收到以下错误:AttributeError: 'Noise' object has no attribute 'img_but'.并且使用command = self.curves没有帮助。在单击按钮之前,我仍然会打开此窗口。 -
我实际上要求从方法中删除
(),self.img_but[image] = self.img有什么问题? -
我不经常使用 OOP,所以我不知道属性有什么问题,但尝试使用
img_but.image = self.img而不是self.img_but.image = self.img,它也应该是command=lambda: self.update_pic(pic) -
@CoolCloud 首先我得到
Unresolved reference 'image' and then the same mistakeAttributeError: 'Noise' object has no attribute 'img_but'`
标签: python user-interface tkinter