Home > other >  X, y is a global variable, and I in the following function on the assignment, why in the following,
X, y is a global variable, and I in the following function on the assignment, why in the following,

Time:09-20

# create GUI window open image, and display in the window
The from PIL import Image, ImageTk
The import matplotlib. Pyplot as PLT
The import tkinter as tk
The from tkinter import filedialog

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# to create window
The window=tk. Tk ()
Window. The title (' main window)
Window. The geometry (' 1000 x600)
# to create canvas, import the picture
Canvas=tk canvas (window, height=600, width=1000)
Canvas. Pack ()
x=None
Y=None
Def choose1 () :
Global x
X=filedialog. Askopenfilename (title=u 'select files,
Reservoir initialdir="D: \ \ photos \ ")

Def choose2 () :
Global y
Y=filedialog. Askopenfilename (title=u 'select files,
Reservoir initialdir="D: \ \ photos \ ")
[/color]



B5=tk. The Button (Windows, width='10', height='2', the text='please select a figure a, the command=choose1)
B5. Place (x=250, y=150, anchor='center')
B6=tk. The Button (Windows, width='10', height='2', the text='please select' figure 2, the command=choose2)
B6. Place (x=750, y=150, anchor='center')

The class ImageObject:
Def __init__ (self, path="") :
The self. The path=path
Try:
self.data=https://bbs.csdn.net/topics/np.array (Image. Open (path))
Except:
self.data=https://bbs.csdn.net/topics/None

Def __add__ (self, other) :
Image=ImageObject ()
Try:
image.data=https://bbs.csdn.net/topics/np.mod (self) data + other) data, 255)
Except:
image.data=https://bbs.csdn.net/topics/self.data
Return the image

Def __sub__ (self, other) :
Image=ImageObject ()
Try:
image.data=https://bbs.csdn.net/topics/np.mod (self. Data - other data, 255)
Except:
image.data=https://bbs.csdn.net/topics/self.data
Return the image

Def __mul__ (self, factor) :
Image=ImageObject ()
Try:
image.data=https://bbs.csdn.net/topics/np.mod (self. Data * factor, 255)
Except:
image.data=https://bbs.csdn.net/topics/self.data
Return the image
Def __truediv__ (self, factor) :
Image=ImageObject ()
Try:
image.data=https://bbs.csdn.net/topics/np.mod (self. Data//factor, 255)
Except:
image.data=https://bbs.csdn.net/topics/self.data
Return the image

Def saveimage (self, path) :
Try:
Im=Image. Fromarray (self. Data)
Im. Save (path)
Return True
Except:
Return False
Print (x)
Print (y)
A=ImageObject (x)
B=ImageObject (y)
(a + b). Saveimage (" D: \ \ photo manager \ \ result_add PNG ")
(a - b). Saveimage (" D: \ \ photo manager \ \ result_sub PNG ")
(a * 2). Saveimage (" D: \ \ photo manager \ \ result_mul PNG ")
(a/2). Saveimage (" D: \ \ photo manager \ \ result_div PNG ")

CodePudding user response:

B5=tk. The Button (Windows, width='10', height='2', the text='please select a figure a, the command=choose1)
-- -- -- -- -- -- -- -- -- --
Command=choose1 said click button command execution,
That is to say, only when you click operation, will trigger choose1 function to perform, so this time will change the global variable x value!
If you don't have to click operation, that, of course, can't see the change global variable x value!

After you click on to print (x) see will change!

============
Choose2 relationship with y, in the same way!