0
我不知道如何將數據從一個類傳輸到另一個類。在下面的代碼中,我使用Tkinter的askdirectory命令導入了一些圖像,並且一旦我有了導入圖像的目錄。然後我得到一些關於數據的信息,例如擴展的數量和圖像的數量。 (我知道這兩個值將是相同的)。我應該提到,這些數據直接在PageOne1類中找到,它將在類PageOne1中調用的函數中處理。如何使用tkinter在Python中將數據從一個類傳輸到另一個類?
一旦這個數據被定義在一個變量中,我需要能夠在一個不同的類中使用它,這個類是一個點擊導入數據的按鈕時提升得更高的框架,這只是讓它看起來不同並且用戶知道發生了一些事情。
問題是: 如何將數據從一個類傳輸到另一個類? E.G,在我的代碼中,我想從PageOne1類傳遞數據給PageOne2。 有了這個被轉移的數據,我想在一個標籤中顯示它。
#structure for this code NOT created by me - found on stackoverflow.com
import tkinter as tk
from tkinter import filedialog as tkFileDialog
import math, operator, functools, os, glob, imghdr
from PIL import Image, ImageFilter, ImageChops
#fonts
TITLE_FONT = ("Helvetica", 16, "bold","underline") #define font
BODY_FONT = ("Helvetica", 12) #define font
#define app
def show_frame(self, c): #raise a chosen frame
'''Show a frame for the given class'''
frame = self.frames[c]
frame.tkraise()
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# the container will contain all frames stacked on top of each other, the frame to be displayed will be raised higher
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne1, PageOne2,):
frame = F(container, self)
self.frames[F] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, c): #raise a chosen frame
'''Show a frame for the given class'''
frame = self.frames[c]
frame.tkraise()
def choose(self):
image_list = []
extlist = []
root = tk.Tk()
root.withdraw()
file = tkFileDialog.askdirectory(parent=root,title="Choose directory")
if len(file) > 0:#validate the directory
print("You chose %s" % file) #state where the directory is
for filename in glob.glob(file+"/*"):
print(filename)
im=Image.open(filename)
image_list.append(im)
ext = imghdr.what(filename)
extlist.append(ext)
print("Loop completed")
extlistlen = len(extlist)
image_listlen = len(image_list)
#these are the two pieces of data I want to transfer to PageOne2
self.show_frame(PageOne2)
#frames
class StartPage(tk.Frame): #title/menu/selection page
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="LTC Meteor Detection Program", font=TITLE_FONT) #using labels as they can be updated at any point without updating the GUI, if the data is to be manipulated by the user canvas will be used
label.pack(side="top", fill="x", pady=10) #pady offers padding between label and GUI border
button1 = tk.Button(self, text="Import Images",
command=lambda: controller.show_frame(PageOne1)) #if button1 chosen, controller.show_frame will raise the frame higher
#lambda and controller being used as commands to raise the frames
button2 = tk.Button(self, text="RMS Base Comparison",
command=lambda: controller.show_frame(PageTwo1))
button3 = tk.Button(self, text="Export Images",
command=lambda: controller.show_frame(PageThree1))
buttonexit = tk.Button(self,text="Quit",
command=lambda:app.destroy())
button1.pack()
button2.pack()
button3.pack()
buttonexit.pack()
class PageOne1(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text = "Import Images", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Select directory",
command=controller.choose)
button.pack()
button = tk.Button(self, text="Return To Menu",
command=lambda: controller.show_frame(StartPage))
button.pack()
#for reference:
#fileName = tkFileDialog.asksaveasfilename(parent=root,filetypes=myFormats ,title="Save the image as...")
class PageOne2(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text = "Import Images", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
label = tk.Label(self, text = ("Number of images: ",image_listlen2," Number of different extensions: ",extlistlen2))
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Return To Menu",
command=lambda: controller.show_frame(StartPage))
button.pack()
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
我不完全確定這是什麼意思,對不起,我知道我可能是愚蠢的。我對Tkinter很陌生。 self.sp_1和self.sp_2是我提供信息的變量嗎?我真的不知道如何使用這個。我想在一個函數中定義一個變量,該變量從一個類中的按鈕調用,然後在另一個類中使用該數據。 – Charles 2014-11-06 21:24:34