Reputation: 13
I launched a small application with tkinter which allows to make a screen shot of the object inside canvas. I am using the PIL library with imagegrab. That works well. But when I freeze the application, and click on the screenshot, the command prompt opens. How can I prevent the command prompt from opening?
The full code: import tkinter as tk # Python 3 tkinter modules
from PIL import Image, ImageTk
from PIL import Image, ImageTk, ImageGrab
class App(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent=parent
file = 'images.jpg'
self.img = Image.open(file)
#self.img.show() #Check to proof image can be read in and displayed correctly.
self.photo = ImageTk.PhotoImage(self.img)
print('size of self.img =', self.img.size)
centerx= self.img.size[0]//2
centery= self.img.size[1]//2
print ('center of self.img = ', centerx, centery)
self.cv = tk.Canvas(self)
self.cv.create_image(centerx, centery, image=self.photo)
self.cv.grid(row=0, column=0, columnspan=3, sticky='nsew')
self.snappic=tk.Button(self, text='SNAP', command=self._snapCanvas)
self.snappic.grid(row=1, column=0, sticky='nsew')
def _snapCanvas(self):
print('\n def _snapCanvas(self):')
canvas = self._canvas() # Get Window Coordinates of Canvas
self.grabcanvas = ImageGrab.grab(bbox=canvas)
self.grabcanvas.show()
def _canvas(self):
print(' def _canvas(self):')
print('self.cv.winfo_rootx() = ', self.cv.winfo_rootx())
print('self.cv.winfo_rooty() = ', self.cv.winfo_rooty())
print('self.cv.winfo_x() =', self.cv.winfo_x())
print('self.cv.winfo_y() =', self.cv.winfo_y())
print('self.cv.winfo_width() =', self.cv.winfo_width())
print('self.cv.winfo_height() =', self.cv.winfo_height())
x=self.cv.winfo_rootx()+self.cv.winfo_x()
y=self.cv.winfo_rooty()+self.cv.winfo_y()
x1=x+self.cv.winfo_width()
y1=y+self.cv.winfo_height()
box=(x,y,x1,y1)
print('box = ', box)
return box
if __name__ == '__main__':
root = tk.Tk()
root.title('App'), root.geometry('300x300')
app = App(root)
app.grid(row=0, column=0, sticky='nsew')
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
app.rowconfigure(0, weight=10)
app.rowconfigure(1, weight=1)
app.columnconfigure(0, weight=1)
app.columnconfigure(1, weight=1)
app.columnconfigure(2, weight=1)
app.mainloop()
Upvotes: 1
Views: 1577
Reputation: 45
The main ways im aware of being able to do this is by making the .py or .pyw file an exe. The other way is to create a python web server for your code.
Upvotes: 0
Reputation: 560
Change your file's extension from .py to .pyw
The only difference with py is that double-clicking the pyw extension's source under Windows will call pythonw.exe to execute the source code. This implementation will not have a command line window. It is mainly used when the GUI program is released without the need to see the console information.
Upvotes: 0