Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中使用tkinter滑块(缩放)值更新图像?_Python_Image Processing_Tkinter_Slider - Fatal编程技术网

如何在python中使用tkinter滑块(缩放)值更新图像?

如何在python中使用tkinter滑块(缩放)值更新图像?,python,image-processing,tkinter,slider,Python,Image Processing,Tkinter,Slider,我是python的初学者。我想做的就是创建一个简单的GUI来实现基本的数字图像处理算法。 我想用tkinter滑块调整图像的亮度。但这不起作用,我也不知道,因为我只是个初学者。我希望scale小部件回调可以做一些亮度调整工作,但由于某些原因,回调没有被调用 这是我的密码: import Tkinter as tk import Image import ImageTk import numpy as np import tkFileDialog class DIP(tk.Frame):

我是python的初学者。我想做的就是创建一个简单的GUI来实现基本的数字图像处理算法。 我想用tkinter滑块调整图像的亮度。但这不起作用,我也不知道,因为我只是个初学者。我希望scale小部件回调可以做一些亮度调整工作,但由于某些原因,回调没有被调用

这是我的密码:

import Tkinter as tk
import Image
import ImageTk
import numpy as np
import tkFileDialog

class DIP(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent) 
        self.parent = parent        
        self.initUI()

    def initUI(self):
        self.parent.title("DIP Algorithms- Simple Photo Editor")
        self.pack(fill = tk.BOTH, expand = 1)

        menubar = tk.Menu(self.parent)
        self.parent.config(menu = menubar)

        self.label1 = tk.Label(self, border = 25)
        self.label2 = tk.Label(self, border = 25)
        self.label1.grid(row = 1, column = 1)
        self.label2.grid(row = 1, column = 2)

        # File Menu
        fileMenu = tk.Menu(menubar)
        menubar.add_cascade(label = "File", menu = fileMenu)

        # Menu Item for Open Image
        fileMenu.add_command(label = "Open", command = self.onOpen)        

        # Basic menu
        basicMenu = tk.Menu(menubar)        
        menubar.add_cascade(label = "Basic", menu = basicMenu)

        # Menu Item for image negative
        basicMenu.add_command(label = "Negative", command = self.onNeg)

        #menu for brightness
        basicMenu.add_command(label = "Brightness", command = self.onBrghtness)

    def onBrghtness(self):
        #Image Brightness Adjustment Menu callback
        brgTk=tk.Tk()
        self.brgSc = tk.Scale( brgTk, from_=-50, to=50, orient=tk.HORIZONTAL, command=self.adjBright, length=200 ,width=10, sliderlength=15)
        self.brgSc.pack(anchor=tk.CENTER)

    def adjBright(self):
        print self.brgSc #***here how can i get slider value, this callback is not being invoked***???

    def onNeg(self):
        #Image Negative Menu callback
        self.I2 = 255-self.I;
        self.im = Image.fromarray(np.uint8(self.I2))
        photo2 = ImageTk.PhotoImage(self.im)
        self.label2.configure(image=photo2)
        self.label2.image = photo2 # keep a reference!

    def setImage(self):
        self.img = Image.open(self.fn)
        self.I = np.asarray(self.img)
        l, h = self.img.size
        text = str(2*l+100)+"x"+str(h+50)+"+0+0"
        self.parent.geometry(text)
        photo = ImageTk.PhotoImage(self.img)
        self.label1.configure(image = photo)
        self.label1.image = photo # keep a reference!

    def onOpen(self):
        #Open Callback
        ftypes = [('Image Files', '*.tif *.jpg *.png')]
        dlg = tkFileDialog.Open(self, filetypes = ftypes)
        filename = dlg.show()
        self.fn = filename
        #print self.fn #prints filename with path here
        self.setImage()

    #def onError(self):
        #box.showerror("Error", "Could not open file")    


def main():
    root = tk.Tk()
    DIP(root)
    root.geometry("320x240")
    root.mainloop()  

if __name__ == '__main__':
    main()
在我提到问题的代码中,回调失败,因为它说了如下内容:

Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args) 
TypeError: adjBright() takes exactly 1 argument (2 given)
请帮忙,我知道这可能是一个愚蠢的问题,但我只是一个初学者,我希望随着时间的推移,我会对PYTHON有深入的理解

回溯(最近一次调用上次):文件 “C:\Python27\lib\lib tk\Tkinter.py”,第1410行,在调用返回中 self.func(*args)TypeError:adjBright()正好接受1个参数(2 (给定)

之所以出现此错误,是因为scale回调始终传递scale的新值。由于您将
adjBright
定义为只有
self
作为参数,Tkinter会抛出一个错误。不幸的是,这似乎是Tkinter的一个记录不足的特性

将另一个参数添加到
adjBright
,则无需担心获取新值:

def adjBright(self, new_value):
    print "the slider value is", new_value

您正在运行哪个版本的Python?我得到了
AttributeError:DIP实例在2.7.3上运行时没有属性“initUI”
错误。@Fenikso这只是不正确的选项卡。您对这个接口的具体意图是什么?是否希望
缩放
小部件仅在单击菜单项“亮度”后显示?它应该随时消失吗?@mmgp-哦,当然。我没有注意到。我已经为你修改了格式。当你注意到问题中有错误时,你可以随时去修改它。哦,伙计,对不起。显然,我对编辑的评论是@bistaumanga。我不知怎么弄错了你的身份。那是一大早:-)。所以就。。。不要介意。