All of the tutorials I have seen deomonstrate the tkinter filedialog.askopenfilename function by only using the information collected within the function that is linked to the tkinter button. I can pass information in the function, but I would like to pass variables (filepath, images, etc.) outside the function and have them update variables in my GUI. I have commented out the location I would like to call the variables in main_gui_setup function below. Any help will be appreciated, as it has been very demoralizing not being able to open a file. If this problem persists, my future as a programmer may be limited to creating tic-tac-toe games or instructional videos for Youtube.
from tkinter import * import tkinter as tk from tkinter import ttk from tkinter import filedialog, messagebox from PIL import ImageTk, Image # was import PIL.Image, PIL.ImageTk import cv2 def main(): root = Tk() window1 = Window(root, "X-ray Assist", "Give up") return None # can't pass by reference in python class Window(): n = 0 file_path = "" img1_info = "" def __init__(self, root, title, message): self.root = root self.root.title(title) #self.root.geometry(geometry) self.screen_width = root.winfo_screenwidth() self.screen_height = root.winfo_screenheight() #self.root.attributes('-topmost', 1) # SET APP WINDOW SIZE scr_size_main = self.scr_size() # create instance of scr_size self.root.geometry("%dx%d+%d+%d" % (self.root_width, self.root_height, self.root_x, self.root_y)) # CREATE MAIN WINDOW GUI create_gui = self.main_gui_setup() self.root.mainloop() pass def scr_size(self): '''Reads monitor size and adjusts GUI frame sizes''' self.root_width = int(self.screen_width*0.52) self.root_height = int(self.screen_height*0.9) self.root_x = int(self.screen_width*0.23) self.root_y = int(self.screen_height*0.02) self.img_ht_full = int(self.screen_height*0.82) self.tools_nb_width = int(self.screen_width*0.22) self.tools_nb_height = int(self.screen_height*0.48) self.hist_nb_width = int(self.screen_width*0.22) self.hist_nb_height = int(self.screen_height*0.23) def open_image(self): main_win = ttk.Frame(self.root) main_win.grid(column=0, row=0) self.file_path = filedialog.askopenfilename(initialdir='/', title='Open File', filetypes=(('tif files', '*.tif'), ('all files', '*.*'))) self.file_path_label = ttk.Label(main_win, text=self.file_path) self.file_path_label.grid(column=0, row=0, columnspan=1, sticky="nw", padx=(5,0), pady=1) self.img1_8bit = cv2.imread(self.file_path, 0) #, cv2.IMREAD_ANYDEPTH | cv2.IMREAD_GRAYSCALE) #self.img1_8bit_resize = cv2.resize(self.img1_8bit, (self.img_ht_full, self.img_ht_full)) #, interpolation = cv2.INTER_CUBIC) #self.img1_height, self.img1_width = self.img1_8bit.shape # not resized for screen #img1_info = text = f"{self.img1_height} {self.img1_8bit.dtype} {self.img1_16bit.dtype}" #print(self.img1_width, " x ", self.img1_height, " bitdepth = ", self.img1_8bit.dtype) #img1_info = ttk.Label #print(f"{self.img1_height} {self.img1_width} {self.img1_8bit.dtype}") #img1_info.grid(column=3, row=1, columnspan=1, sticky="w", padx=(5,0), pady=1) #img = io.imread(main_win.filename) #scikit self.img1_16bit = cv2.imread(self.file_path, cv2.IMREAD_ANYDEPTH | cv2.IMREAD_GRAYSCALE) #self.img_canvas = tk.Canvas(self.root, width=self.img_ht_full, height=self.img_ht_full) #self.img_canvas.grid(column=1, row=2, columnspan=10, rowspan=10, sticky="nw") #self.img_canvas.image = ImageTk.PhotoImage(image=Image.fromarray(self.img1_8bit_resize)) #self.img_canvas.create_image(0,0, image=self.img_canvas.image, anchor="nw") # .create_line(x1, y1, x2, y2, fill="color") #self.img_canvas.create_line((self.img_ht_full/2), 0, (self.img_ht_full/2), (self.img_ht_full), fill="yellow") #self.img_canvas.create_line(0, (self.img_ht_full/2), (self.img_ht_full), (self.img_ht_full/2), fill="yellow") def main_gui_setup(self): main_win = ttk.Frame(self.root) main_win.grid(column=0, row=0) image_win = ttk.Frame(main_win, borderwidth=25, relief="groove", width=self.img_ht_full, height=self.img_ht_full) image_win.grid(column=1, row=2, columnspan=10, rowspan=10, sticky="nw") toolbar = ttk.Frame(main_win, borderwidth=5) #, width=1100, height=15) toolbar.grid(column=0, row=0, columnspan=10, rowspan=1, sticky="nw", padx=20) hist_win = ttk.Frame(main_win, borderwidth=25, relief="groove", width=300, height=200) panel_info = ttk.Label(main_win, text=f"{self.screen_width} x {self.screen_height}") panel_info.grid(column=5, row=1, columnspan=1, sticky="e", pady=1) # SCROLL SLIDER AT BOTTOM slider = ttk.Scrollbar(main_win, orient="horizontal") slider.grid(column=1, row=13, columnspan=7, padx=5, pady=5, sticky="ew") #X-RAY AND DETECTOR SETTINGS - will input these from separate class kv = ttk.Label(main_win, text="125kV") kv.grid(column=0, row=2, columnspan=1, padx=15, pady=5) file_path_label = ttk.Label(main_win, text="No image loaded") file_path_label.grid(column=1, row=1, columnspan=1, sticky="nw", padx=(5,0), pady=1) # CREATE BUTTONS open = ttk.Button(toolbar, text="Open", width=10, command=self.open_image) open.grid(column=0, row=0) save = ttk.Button(toolbar, text="Save", width=10) save.grid(column=1, row=0) b1 = ttk.Button(toolbar, text="1", width=10) b1.grid(column=2, row=0) b2 = ttk.Button(toolbar, text="2", width=10) b2.grid(column=3, row=0) pass main()
https://stackoverflow.com/questions/67038914/how-can-i-return-a-variable-from-a-function-that-is-opening-a-file-using-filedia April 11, 2021 at 04:24AM
没有评论:
发表评论