"機能ボタンの設定"という
コメント周辺にラジオボタンを実装しています。
この2つのボタンを使って以下の機能を実装したいのですが上手くいかず、困っています。
実装したい機能
ラジオボタンを"二値化処理"に切り替えるたびに、testが表示される
原因を教えていただけますと、助かります。
エラーの内容として、以下の内容が出現します。
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\kuron\anaconda3\lib\tkinter_init_.py", line 1883, in call
return self.func(*args)
TypeError: 'NoneType' object is not callable
# -*- coding: utf-8 -*- import os import sys import tkinter as tk import tkinter.filedialog as tkf import tkinter.ttk as ttk from PIL import Image, ImageTk import math import cv2 import numpy as np radiobutton_text = ['二値化処理', 'test'] x_interval = 10 y_interval = 30 class Application(tk.Tk): def __init__(self): super().__init__() # rootメインウィンドウの設定 self.minsize(width = 600, height = 400) self.title("画像処理") self.geometry("1200x960+0+0") self.thread = None self.canvas_width = 1080 self.canvas_height = 720 # メインフレームの作成と設置 mainframe = ttk.Frame(self) mainframe.place(x=0, y=0) # 表示する画像の設定 self.canvas = tk.Canvas( self, width = self.canvas_width, height= self.canvas_height, bg = "black") self.canvas.place(x=0, y=0, anchor = tk.NW) self.canvas_obj = None self.image_cv = None y_interval_num = 0 # readボタンの設定 self.button = tk.Button( self, text = "画像読込", command = self.readFile ).place(x = self.canvas_width + x_interval, y=y_interval * y_interval_num) y_interval_num += 1 # saveボタンの設定 self.button = tk.Button( self, text = "画像保存", command = self.saveFile ).place(x=self.canvas_width + x_interval, y=y_interval * y_interval_num) y_interval_num += 1 # 機能ボタンの設定 try: rdo_var = tk.IntVar() for i in range(len(radiobutton_text)): self.radiobutton = tk.Radiobutton( self, value=i, variable= rdo_var, text= radiobutton_text[i] ).place(x=self.canvas_width + x_interval, y=y_interval * y_interval_num) y_interval_num += 1 function_mode = rdo_var.get() rdo_var.trace("w", self.functionCollection(function_mode)) except Exception as e: print(str(sys.exc_info()[0])) print(str(sys.exc_info()[1])) print(str(sys.exc_info()[2])) # 画像読込ボタンの設定 def readFile(self): # 読み込むファイルの種類と開始場所を決める fileType = [("", "*.png;*.jpg")] iDir = os.path.abspath(os.path.dirname(__file__)) readfilename = tkf.askopenfilename( filetypes = fileType, initialdir = iDir) if (readfilename == ''): return image_cv = cv2.imread(readfilename) # ファイルのリサイズ height, width, _ = image_cv.shape[:3] while(1): if(width > self.canvas_width or height > self.canvas_height): width = width * 0.99 height = height * 0.99 else: break image_cv = cv2.resize( image_cv, dsize = (math.floor(width), math.floor(height))) # cvからPILへ変換 image_rgb = cv2.cvtColor(image_cv, cv2.COLOR_BGR2RGB) self.baseimage = Image.fromarray(image_rgb) self.tk_image = ImageTk.PhotoImage(self.baseimage) if self.canvas_obj != None: self.canvas_obj = None self.canvas_obj = self.canvas.create_image( 0, 0, image = self.tk_image, anchor="nw" ) # 画像保存ボタンの設定 def saveFile(self): if self.canvas_obj != None: savefilename = tkf.asksaveasfilename( filetypes = [("JPEG", ".jpg")]) if (".jpg" not in savefilename ): savefilename = savefilename + ".jpg" image_rgb = np.asarray(self.baseimage) image_bgr = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2BGR) cv2.imwrite(savefilename, image_bgr) def functionCollection(self, function_mode): if function_mode == 0: result = self.binallization() elif function_mode == 1: pass def binallization(self): try: print('test') except Exception as e: print(str(sys.exc_info()[0])) print(str(sys.exc_info()[1])) print(str(sys.exc_info()[2])) def main(): app = Application() app.mainloop() if __name__ == '__main__': main()
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/10/17 06:11
2021/10/17 14:58
2021/10/19 02:45