0

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

这是我的代码:

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 有深入的了解......

4

1 回答 1

2

回溯(最近一次调用最后):文件“C:\Python27\lib\lib-tk\Tkinter.py”,第 1410 行,调用返回 self.func(*args) 类型错误:adjBright() 正好采用 1 个参数(2给定)

您收到此错误是因为比例回调始终传递了比例的新值。由于您将其定义adjBright为仅self作为参数,因此 Tkinter 会引发错误。不幸的是,这似乎是 Tkinter 的一个记录不足的功能。

添加另一个参数adjBright,然后您不必担心获取新值:

def adjBright(self, new_value):
    print "the slider value is", new_value
于 2013-01-14T20:51:21.583 回答