Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

How to make a Python script run without it opening the console window with tkinter?

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?

console image

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('
 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()
question from:https://stackoverflow.com/questions/66053232/how-to-make-a-python-script-run-without-it-opening-the-console-window-with-tkint

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...