Picture disappear after change in Tkinter - python-3.x

I have this problem: I created this program with Python3 and Tkinter, that show the picture of a place (like mountain or sea) and the program speak what is the place shown in the picture. The problem is when the picture change, it only appears for a few moments, just the time the program say what place is, and the disappear and remain only the label where it is written what represent the picture.
This is the code:
from tkinter import *
from PIL import Image,ImageTk
import pyttsx3
root = Tk()
root.title("Try change image")
root.geometry("1000x600")
root.resizable(height=FALSE,width=FALSE)
Leftframe = Frame(root)
Leftframe.pack(side = LEFT)
Rightframe = Frame(root)
Rightframe.pack(side=RIGHT)
engine = pyttsx3.init()
voices = engine.getProperty('voices')
rate = engine.getProperty('rate')
engine.setProperty('rate', rate -20)
for voice in voices:
engine.setProperty('voice',voice.id)
def start():
global back_label,label
back = ImageTk.PhotoImage(file=directory mountain picture)
back_label = Label(Leftframe,image = back)
back_label.pack()
label=Label(Rightframe,text="Mountain")
label.pack()
root.update()
engine.say("Mountain")
engine.runAndWait()
def change():
back_label.destroy()
label.destroy()
back = ImageTk.PhotoImage(file=directory sea picture)
back_label1 = Label(Leftframe,image = back)
back_label1.pack()
label1 = Label(Rightframe,text="Sea")
label1.pack()
root.update()
engine.say("Sea")
engine.runAndWait()
start()
change()
root.mainloop()
I hope you understand what I want to do, every help will be appreciated

Related

button to take frame screenshot

can someone help me take a screnshot of a specific frame?
been playing with these, but cant seem to specify just the frame
import pyautogui
#im1 = pyautogui.screenshot()
#im1.save('my_screenshot.png')
#im2 = pyautogui.screenshot('my_screenshot2.png')
from tkinter import *
import time
from PIL import ImageTk, Image
import pyautogui as pg
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x350")
frm2shoot = Frame(win)
frm2shoot.grid(column=0, row=0)
lbl = Label(frm2shoot, width=16, text="testing testing:", justify=LEFT, anchor="w").grid(row=0, column=0, sticky=W, pady=2)
# Define a function for taking screenshot
def screenshot():
random = int(time.time())
filename = "/Users/ricardosimoes/Desktop/"+ str(random) + ".jpg"
ss = pg.screenshot(filename)
ss.show()
frm2shoot.deiconify()
# Create a Button to take the screenshots
button = Button(win, text="Take Screenshot", font=('Aerial 11 bold'), background="#aa7bb1", foreground="white", command=screenshot)
button.grid(column=5, row=0)
win.mainloop()
Anyone have any idea how to do this??

Changing order of execution in python

I have recently started programming and written a fairly simple program. But stuck at a point. My program does a very simple thing that when you click a button it will say a line and shows that text on the screen. But it is speaking the text first and then displays it on the screen but I want its reverse, i.e it should first display it on the screen and then speak the text.
from tkinter import *
import pyttsx3
root = Tk()
def speak():
engine = pyttsx3.init()
say = "I am speaking."
text.insert("0.0", say)
engine.say(say)
engine.runAndWait()
text = Text(root)
text.pack()
btn = Button(root, text="speak", command=speak)
btn.pack()
root.mainloop()
Thanks in advance.
It is because engine.runAndWait() blocks the application, and so text box cannot be updated by tkinter mainloop until the speaking completed.
You can call text.update_idletasks() to force tkinter to update the text box before the speaking:
engine = pyttsx3.init() # initialize only once
def speak():
say = "I am speaking."
text.insert("0.0", say)
text.update_idletasks() # force tkinter update
engine.say(say)
engine.runAndWait()
You could delay the call to engine.say
from tkinter import *
import pyttsx3
def speak(sentence):
engine.say(sentence)
engine.runAndWait()
def display_and_speak():
sentence = "I am speaking."
text.insert("1.0", sentence)
root.after(1000, speak, sentence) # call to speak delayed 1 second
engine = pyttsx3.init()
root = Tk()
text = Text(root)
text.pack()
btn = Button(root, text="speak", command=display_and_speak)
btn.pack()
root.mainloop()

How do I place a text entry in a colored frame in python using tkinter?

I am making a wireframing tool desktop application for MacOS in python with Tkinter and I have no idea how to have a text entry bar that I can put in a frame that has a background color of black.
I looked up how to try and do this task, but had no luck. I have also tried to ask my coding class teacher if he can help me with this, but he couldn't figure it out either. Can someone try to help me with this?
Here is what I have so far:
from tkinter import *
root = Tk()
root.geometry("1430x840")
def clear_entry(entry):
entry.delete(0, END)
// here is the text entry
entry = Entry(root)
placeholder_text = 'Title'
entry.insert(0, placeholder_text)
entry.bind("<Button-1>", lambda event: clear_entry(entry))
// here is the frame which I want to put it in
frame2 = Frame(root, width=1430, height=56, bg="#292E30")
frame2.pack()
entry.pack(side=TOP, anchor=N)
root.mainloop()
the end result of what I want, this is an edited image with Preview so I can show you what I want it to look like in the end.
Just move your frame2 up and have the entry master set to frame2. To acquire a fully stretched black frame, pass a few more parameters to your pack method:
from tkinter import *
root = Tk()
root.geometry("1430x840")
frame2 = Frame(root, bg="#292E30")
frame2.pack(fill=X,ipady=15)
def clear_entry(entry):
entry.delete(0, END)
entry = Entry(frame2)
placeholder_text = 'Title'
entry.insert(0, placeholder_text)
entry.bind("<Button-1>", lambda event: clear_entry(entry))
entry.pack(side=LEFT, anchor=N)
root.mainloop()

Trouble adding Images into a Gui using tkinter

OK so i am trying to make a program that displays an image when pressing a button, and i am having trouble getting the images into the program
this is my full code:
# Nicolas Bart
import tkinter as tk
from PIL import Image, ImageTk
from tkinter import *
window = tk.Tk()
window.title('Bad Meme Generator')
window.geometry('500x500')
window.configure(bg='saddle brown')
meme_label = tk.Label(window, text='PRESS BUTTON FOR BAD MEMES:',
fg='blue4', bg='brown4', font=('comicsans', '20'))
meme_label.grid(pady=25, padx=25, column=0, row=0)
def button_command():
meme_window = tk.Tk()
meme_window.title('I Warned You')
meme_window.grid()
image = Image.open('pexels-photo-247932.jpg')
photo = ImageTk.PhotoImage(image)
label = tk.Label(meme_window, image=photo)
label.image = photo
label.place(x = 0, y = 0)
button = tk.Button(window, text='Dont Do It!', command=button_command,
padx=100, pady=75, font=('comicsans', '20'),
bg='brown4', fg='blue4')
button.grid(column=0, row=1)
warning_label = tk.Label(window, text="Really shit tier memes incoming:",
bg='brown4', fg='blue4',
font=('comicsans', '20'))
warning_label.grid(pady=75)
window.mainloop()
every time i run this program, when i press the button to open the image, it gives the error "AttributeError: type object 'Image' has no attribute 'open'"
the specific part of the program that is giving the error is:
def button_command():
meme_window = tk.Tk()
meme_window.title('I Warned You')
meme_window.grid()
image = Image.open('pexels-photo-247932.jpg')
photo = ImageTk.PhotoImage(image)
label = tk.Label(meme_window, image=photo)
label.image = photo
label.place(x = 0, y = 0)
any help would be appreciated. Thank you :)
This is a good example of why you shouldn't do from tkinter import *. Tkinter has an Image class, so by doing this import after importing Image from PIL you overwrite the PIL class with the tkinter class.
Since you're already importing tkinter the preferred way (import tkinter as tk), you don't need to import tkinter a second time. You need to remove the statement from tkinter import *.
You also make the mistake of creating more than one instance of Tk. I don't know if it contributes to the problem or not, but it's not something you should be doing. If you need additional windows then you should create instances of Toplevel.

Getting function NOT to run on import

I have tried EVERY " if name=='main' " suggestion and it still fails (or maybe I'm doing it wrong or not understanding it). I am also Not trying to run things from a command line. While testing things out in Pycharm, I am trying to get the main file to pop-up with a gui and then when I click on the button, trying to get the first window to go away and the second file/gui to pop up in its place.
Any help would be greatly appreciated.
this is the main one I am trying to start with but opens up with the second gui first
from tkinter import *
from tryingstuff import EmdMain
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
#firebutton
self.fphoto = PhotoImage(file="../icon/fireorig.png")
self.fbutton = Button(frame, image=self.fphoto)
self.fbutton.config( height=228, width=200)
self.fbutton.pack(side=LEFT)
#emsbutton
self.mphoto = PhotoImage(file="../icon/ems.png")
self.emsButton = Button(frame, image=self.mphoto, command=EmdMain)
self.emsButton.config( height=224, width=197)
self.emsButton.pack(side=RIGHT)
root = Tk()
root.title("ProQA Card set")
app = App(root)
root.mainloop()
This is the second file
from tkinter import *
def EmdMain():
frame = Frame()
frame.pack()
abdominalPnB = Button(frame, text="01_Abdominal Pain")
abdominalPnB.config(anchor="w", width=20, height=1)
abdominalPnB.grid(row=0, column=0)
root = Tk()
app = EmdMain()
root.mainloop()
if __name__=='__main__':
EmdMain()

Resources