I have a simple code block that displays an image when the user press a button. When I save the script with .py extension, there is console at the background so I decided to save it as .pyw to hide it. Here is my code;
from tkinter import *
from PIL import Image
def open_image():
im = Image.open("tobi.jpg")
im.show()
root = Tk()
root.geometry("800x600+400+300")
buton = Button(root)
buton.config(text = "Show the image", command = open_image, activebackground = "yellow", bg = "lightgreen")
buton.pack()
mainloop()
I don't see cmd at the background since I save it as .pyw. However, when I click the button, just before opening the image I see console for a short time, then it dissappears. How can I avoid this, I want to hide console completely,
I dont know how to do this through pure tk but if subprocess is an option then you can do the following. Not a very good solution, since you have to use the subprocess module just to open an image... but anyways....
import tkinter as tk
import subprocess as s
root = tk.Tk()
def open_image():
s.call(["tobi.jpg"], shell=True)
button = Button(root)
button.config(text="Show the Image", command=open_image, activebackground="yellow", bg="lightgreen")
button.pack()
root.mainloop()
alternatively you could use os and use os.system("start my_file_name.jpg")
(assuming windows here)
With os.system the console will show but only very briefly.
Really doing the same thing, opening with PIL is just slower than others, so you see the console for a longer period.
U can use the following code:`
import win32console
import win32gui
win=win32console.GetConsoleWindow() # For closing command window
win32gui.ShowWindow(win,0)
This will close the cmd window at start itself, and will not show again. The cmd window may show for a very little time at first, as a blink, then it will not show. u can save in .py extention.
here the full code:`
from Tkinter import *
from PIL import Image
import win32console
import win32gui
win=win32console.GetConsoleWindow() # For closing command window
win32gui.ShowWindow(win,0)
def open_image():
im = Image.open("tobi.jpg")
im.show()
root = Tk()
root.geometry("800x600+400+300")
buton = Button(root)
buton.config(text = "Show the image", command = open_image, activebackground = "yellow", bg = "lightgreen")
buton.pack()
mainloop()
Related
Working in a EXE file with background and Icon embed, I read some ideas about to use img2py in order to create module.py that can be imported into the EXE with pyinstaller.
I successfully create the imagebg.py (BG2.png Background) and imageico.py (ico2.ico Icon) modules using img2py. But there is no example or way to set that images into the modules in the tkinter label as background and the icon into the code.
Please anybody can help me.
Main code where must be imported BG ad ICON modules and use them as background and Icon images for tkinter GUI
#Main code GUI tkinter
import imagebg #from here we must import PNG_File.png for Background
import imageico #from here we must import ICO_File.ico for Icon
import wx #from img2py installation
from tkinter import *
root=Tk()
#set windows size
root.resizable(width=False, height=False)
root.geometry("925x722")
#set title
root.title("SOFT1)")
#frame 1
f1=Frame(root, width=345,height=475,bg="light
grey",highlightbackground="black",highlightthickness=4)
f1.place(x=20,y=235)
#set a image as BG
Logo=PhotoImage(file="PNG_File.png")
lab6=Label(root, image=Logo)
lab6.place(x=0, y=0)
#set a image as ICON
root.iconbitmap("ICO_File.ico")
mainloop()
The module imagebg.py for backgroud generated with IMG2PY
BG2 = PyEmbeddedImage(
b'iVBORw0KGgoAAAANSUhEUgAAAsUAAAKECAIAAABgrdCGAAAACXBIWXMAAA7EAAAOxAGVKw4b'
b'AAAgAElEQVR4nOzdd2Ab5d0H8Oe0hy1vecYjcZw4cfYimwQIEEbYhQJlFgqU3UFZLR2M9i2l'
b'tLRlFcouI5Qww15hZJEdO4kdOx7xtiVrj7v3DwdFlmXppu4kfT9/Wfbdc78YY339TMrpdBIA'
b'AAAAAVRyFwAAAABJD3kCAAAAhEKeAAAAAKGQJwAAAEAo5AkAAAAQCnkCAAAAhEKeAAAAAKGQ'
b'JwAAAEAo5AkAAAAQCnkCAAAAhEKeAAAAAKGQJwAAAEAo5AkAAAAQCnkCAAAAhEKeAAAAAKGQ'
b'JwAAAEAo5AkAAAAQCnkCAAAAhEKeA..... to much characters to be placed in this post
The module imageico.py for icon generated with IMG2PY
from wx.lib.embeddedimage import PyEmbeddedImage
ico2 = PyEmbeddedImage(
b'iVBORw0KGgoAAAANSUhEUgAAAsUAAAKECAYAAADvz0fRAAAABHNCSVQICAgIfAhkiAAAIABJ'
b'REFUeJzs3WeAVNXdBvDnTi/b+7L0svSmIIKd2Gus0ZhiSeKreZOYmGpL15i8SdTEFKMpGkss'
b'GEuMqNFYsCAgHRZYWFiWsn12p7f7fkBwy8zszC1z7537/L7ozs6c82dh4dkz/3OOEAgERBAR'
b'ERERmZhF6wKIiIiIiLTGUExEREREpsdQTERERESmx1BMRERERKbHUExEREREpsdQTERERESm'
b'x1BMRERERKbHUExEREREpsdQTERERESmx1BMRERERKbHUExEREREpsdQTERERESmx1BMRERE'
b'RKbHUExEREREps.... to much characters to be placed in this post
Please any idea of how to use the modules as images for background and icon in the main code GUI Tkinter
You need to convert the wx.Image to PIL compatible image if you want to use it in tkinter application.
Below is an example based on your posted code:
import tkinter as tk
from PIL import Image, ImageTk
import imagebg
import imageico
# function to convert a wx.Image to PIL.ImageTk.PhotoImage
def wx2pil(wx_image):
image = wx_image.Image
w, h = image.GetWidth(), image.GetHeight()
data = image.GetData()
img = Image.frombytes('RGB', (w, h), bytes(data))
return ImageTk.PhotoImage(img)
root = tk.Tk()
root.resizable(False, False)
root.geometry('925x722')
root.title('SOFT1')
# use iconphoto() instead of iconbitmap()
root.iconphoto(False, wx2pil(imageico.ico2))
#set a image as BG
logo = wx2pil(imagebg.BG2)
tk.Label(root, image=logo).place(x=0, y=0)
#frame 1
f1 = tk.Frame(root, width=345, height=475, bg='lightgray', highlightbackground='black', highlightthickness=4)
f1.place(x=20, y=235)
root.mainloop()
Note that I use wxPython v4.1.0.
As it depends on wxPython, why don't you use wxPython for the GUI directly instead of tkinter?
I am building a program for Windows PCs that contains a lot of buttons and seems very plain. So I was wondering, can I make it so when you push a button (using tkinter), can I play a sound to liven up the program a bit? Please keep in mind I am learning so please dumb it down a bit.
Assuming your file is a WAV:
from tkinter import *
from winsound import *
root = Tk() # create tkinter window
play = lambda: PlaySound('Sound.wav', SND_FILENAME)
button = Button(root, text = 'Play', command = play)
button.pack()
root.mainloop()
Assuming your file is a MP3:
from Tkinter import *
import mp3play
root = Tk() # create tkinter window
f = mp3play.load('Sound.mp3'); play = lambda: f.play()
button = Button(root, text = 'Play', command = play)
button.pack()
root.mainloop()
You might want to consider using pygame as a cross-platform alternative to winsound.
import tkinter as tk
from pygame import mixer
mixer.init()
sound = mixer.Sound("sound.ogg")
root = tk.Tk()
tk.Button(root, command=sound.play).pack()
root.mainloop()
Refer to the docs for more information.
You first need to link the click of your mouse on the image, with an even handler, then simply define an on_click function:
def on_click(event):
winsound.Beep('frequency', 'duration')
Here you can find more information about playing sounds in python.
Just use
import os
os.system("play sound.mp3")
I have a python tkinter app script which can take screenshots on pressing the button. But unfortunately while taking the screenshot, the window of the app also gets captured.
Here's the code of the app:
from PIL import ImageGrab
import tkinter as tk
from tkinter import filedialog
import time
def takeShot():
sc = ImageGrab.grab()
path = filedialog.asksaveasfilename(defaultextension='.png')
sc.save(path)
root = tk.Tk()
canvas1 = tk.Canvas(root,width=300,height=300)
canvas1.pack()
but = tk.Button(text='Take Screenshot',command=takeShot,bg='green',fg='white',font=10)
canvas1.create_window(150,150,window=but)
root.mainloop()
Though it takes the screenshot but app window also gets captured
I don't want that white window in the screenshot. I tried to minimize the window and then maximise by updating the function like this:
def takeShot():
root.withdraw()
sc = ImageGrab.grab()
root.deiconify()
path = filedialog.asksaveasfilename(defaultextension='.png')
sc.save(path)
but it didn't work.
Is there anyway to minimize the app then take the screenshot and then maximize it for save dialogue box so that fulscreen gets captured ??
You need to call root.update_idletasks() after the call to root.withdraw() to force tkinter to redraw the window (or in this case, to remove the window from view).
If you find that root.update_idletasks() doesn't cause the window to hide on your platform, you can try root.update(). update_idletasks should work, though.
I don't have the issue in my Windows running Python 3.8.1. However, I suggest to move the asksaveasfilename(...) right after root.withdraw() as asksaveasfilename(...) is a modal dialog and returns after it is closed:
def take_snapshot():
root.withdraw()
filename = filedialog.asksaveasfilename(initialdir='.', defaultextension='.png')
if filename:
image = ImageGrab.grab()
image.save(filename)
root.deiconify()
Script2 displays an image and and closes the image after spacebar is being pressed. Script2 works perfectly alone.
Unfortunately when I use script1 to launch script2, I have to click/refocus the image window before I'm able to close the image with spacebar.
I've tried using win32gui module to refocus generated image window with separate script (focus.py). If I run focus.py by myself when the image is being displayed, I'm able to just press the spacebar and image window closes. However I've not been able to import focus.py script to script2.py succesfully (so that I would only have script1.py and script2.py.
script1:
action = raw_input("Pick a scenario: ")
if action == "1":
execfile("script2.py")
else:
print "Try again."
return self
script2:
import os, sys
import Tkinter
from PIL import Image, ImageTk
def close_window(event):
event.widget.quit()
root = Tkinter.Tk()
root.bind("<space>", close_window)
imagefile = "image1.png"
image1 = Image.open(imagefile)
root.geometry('%dx%d' % (image1.size[0],image1.size[1]))
tkpi = ImageTk.PhotoImage(image1)
label_image = Tkinter.Label(root, image=tkpi)
label_image.place(x=0,y=0,width=image1.size[0],height=image1.size[1])
root.mainloop()
focus.py
import win32gui
tk = win32gui.FindWindow(None, 'tk')
win32gui.SetForegroundWindow(tk)
I want to launch an "Open File" dialog in Tkinter in Python 2.7.
My code starts with:
from Tkinter import Frame, Tk, BOTH, Text, Menu, END
import tkFileDialog as tkfd
import fileinput
root = Tk()
global strTab
strTab = ""
def openTab(event):
r = tkfd.askopenfilename()
strTab = unicodedata.normalize('NFKD', r).encode('ascii','ignore')
Later in the code I have:
btnLoadTab = Button(root,
text="Load Tab",
width=30,height=5,
bg="white",fg="black")
btnLoadTab.bind("<Button-1>", openTab)
btnLoadTab.pack()
root.mainloop()
When I press the button an "Open File" dialog is shown, but when I select a file it closes and the button remains "clicked".
If I later call to strTab outside of openTab, it remains equal to "".
You can find workable example here: http://www.python-course.eu/tkinter_dialogs.php