我们正在尝试从Windows中的资源管理器窗口获取文件路径,但是当我们运行我们的类来使用文件路径时,它会因错误而崩溃:
IOError: [Errno 22] invalid mode ('rb') or filename: ''Exception in Tkinter callback
我认为错误与我们存储文件路径的方式有关
这是我们的代码:
import Tkinter
from Tkinter import Tk, Label, Button
import subprocess
import sys
import tkFileDialog
import PIL
import matplotlib.pyplot as plt # single use of plt is commented out
import os.path
import PIL.ImageDraw
import PIL.Image
bg_size = [1080,1920]
logo_size = [200,200]
seal_size = [200,200]
wink = ""
wow = ""
seal = 0
class Political_Logo:
def __init__(self, master):
self.master = master
master.title("A simple GUI")
self.label = Label(master, text="Picture Logo")
self.label.pack()
self.label = Label(master, text="Banner")
self.label.pack()
self.find_button = Button(master, text="Find Campaign Background", command=self.OpenExplorer)
self.find_button.pack()
self.find_button = Button(master, text="Find Campaign Logo", command=self.OpenExplorer1)
self.find_button.pack()
self.find_button = Button(master, text="Lets Go", command=lambda: self.test(wow,wink))
self.find_button.pack()
def OpenExplorer(self):
def main():
Tkinter.Tk().withdraw() # Close the root window
in_path = tkFileDialog.askopenfilename()
wow = in_path
print wow , "\n"
if __name__ == "__main__":
main()
def OpenExplorer1(self):
def main():
Tkinter.Tk().withdraw() # Close the root window
in_path1 = tkFileDialog.askopenfilename()
wink = in_path1
print wink , "\n"
if __name__ == "__main__":
main()
def find_filename(substring, self):
import os
import re
files = os.listdir(os.getcwd())
for file in files:
search = re.search(substring, file)
if search:
filename = file
break
return filename
def test(main_image, logo_image, self):
logo_image = PIL.Image.open(logo_image, 'r')
main_image = PIL.Image.open(main_image, 'r')
img_w, img_h = main_image.size
if seal == 0:
print("We converting a banner its lit")
offset = (0,780)
main_image.paste(logo_image, offset)
main_image.save('out.png')
main_image.show()
else:
logo_image.resize(seal_size)
offset = (0,600)
print("We are converting a seal over here")
root = Tk()
my_gui = Political_Logo(root)
root.mainloop()
这是我们的堆栈跟踪:
File "C:\Users\XXX\Desktop\1.4.7-Project\GUI.py", line 41, in <lambda>
self.find_button = Button(master, text="Lets Go", command=lambda: self.test(wow,wink))
File "C:\Users\XXX\Desktop\1.4.7-Project\GUI.py", line 69, in test
logo_image = PIL.Image.open(wow, 'r')
File "C:\Users\XXX\Desktop\User\lib\site-packages\PIL\Image.py", line 2288, in open
fp = builtins.open(fp, "rb")
IOError: [Errno 22] invalid mode ('rb') or filename: ''
Exception in Tkinter callback