not displaying image in Tkinter [duplicate] - python

This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Tkinter PIL image not displaying inside of a function
(1 answer)
Closed 2 years ago.
So I'm experimenting With Tkinter message boxes And I'm trying to insert an image in my toplevel window>
from tkinter import*
from tkinter import messagebox
root = Tk()
root.title('Hi')
root.iconbitmap('C:/Users/davids/Downloads/i2.ico')
e = Entry(root,borderwidth=8,width=35)
top = Toplevel()
def popup():
mg = messagebox.askyesno("Warning", "Click yes/No.")
if mg == True:
top = Toplevel()
top.geometry('1200x1200')
b= Label(top, text="hi")
b.pack()
image = PhotoImage(file="path")
Label(root, image=image).pack()
Button(root,text="Ask question", command=popup).pack()
root.mainloop()
However when I run this code nothing shows. There isn't an error so I can't see what I'm doing wrong. Only the text is displayed. I've tried adding root. but that still doesn't fix it. Please note that I do know that it is easier to use PIL however my new operating system won't install it for some reason so I'm trying to find a way without Pillow. Any idea what is happening?

Related

Is there a way to have a message box on screen while other code runs in the background? (Python) [duplicate]

This question already has answers here:
Tkinter understanding mainloop
(3 answers)
How do you run your own code alongside Tkinter's event loop?
(5 answers)
Closed 12 days ago.
I'm working on a script that requires the program to continue running while a message box is on screen. I want it to still do my program, but have a message box that appears while the script runs
I've been using Tkinter for the message boxes, and I can make multiple appear on screen. However, if I try to run code while the message boxes are on screen, it doesn't work.
Here's my code:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.withdraw()
for i in range(0,5):
f = tk.Toplevel(root)
f.resizable(False, False)
f.geometry("150x75+100+100")
f.title("MessageBox")
ttk.Label(f, text="This is a Test").pack()
print("test")
root.mainloop()
while True:
print("doing code while there are msgboxes on screen")
What I want it to do is draw the message boxes on my screen and continue the code as if I hadn't.
Any help would be much appreciated, thanks!

How can I detect if a user click a tkinter button? [duplicate]

This question already has answers here:
How to handle a click button event in python module tkinter
(2 answers)
Closed 2 years ago.
Here is my code!
///
from tkinter import *
import os
import pygame
os.system(‘clear’)
#Window Setup
root = Tk()
root.title(‘cottontail’)
root.geometry(‘800x600’)
frame = Frame(root)
title_screen = Label(root, text = “Choose your rabbits name!”)
title_screen.pack()
name = Text(root, width=10 , height=3)
name.pack()
confirm_name = Button(root, text= “Conirm?”, width = 5, height=3)
root.mainloop()
///
My objective is to take the input the user puts in the text box to make a label in a pygame window with that name. I figured that a button would be an easy way to confirm the name and open the pygame screen. If this makes any sense to you it would really be appreciated if you could help me. Hope you have a good night!
Thanks in advance!
I'm not familiar with tkinter as I'm a PyQt dev, but I can help with your question as the logic behind both of them are same.
You can make a function that is called by the button whenever its clicked, and then, inside that function definition, do whatever you want.
Here's the website that explains this in detail -
https://www.delftstack.com/howto/python-tkinter/how-to-get-the-input-from-tkinter-text-box/

Tkinter: Pyimage is not working in a Function [duplicate]

This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 5 years ago.
I was trying to code a small window in a program that is giving you feedback about your statistics in the game. The output is working fine, but the image i tried to edit is not working. I tried to define the image global, i tried to take the direct path, i tried to change the suffix of the picture i want to edit (right now it's a .gif -file) but nothing is working. Whats wrong? What is my mistake?
import tkinter
def blackhole():
Black=tkinter.Tk()
Black.title("BlackHole")
schrift=tkinter.Label(Black,text="BlackHole: Game got reseted!")
schrift.pack()
Medal=tkinter.Label(Black,text="Congretulation! You earn the Bronze Medal!")
Medal.pack()
knopf=tkinter.Button(Black,text="Ok",command=submit)
knopf.pack()
canvas = tkinter.Canvas(Black, width=250, height=250)
canvas.pack()
tk_img = tkinter.PhotoImage(file = '/Users/Hannes/Desktop/Klickbot/b.gif')
canvas.create_image(150, 150, image=tk_img)
To include images with Tkinter you should use Pillow (PIL).
https://python-pillow.org/
Run pip install Pillow from the terminal to install.
Sample usage:
from PIL import ImageTk, Image
img = ImageTk.PhotoImage(Image.open("logo.png"))
panel = Label(root, image=self.img)
panel.pack(side="top", fill="both", expand="yes")

How to create a window with Python [duplicate]

This question already has answers here:
Easiest way to develop simple GUI in Python [closed]
(7 answers)
Closed 6 years ago.
I am learning to program with Python 3.6, and would like to ask for help on building a window. It would be greatly appreciated if someone would please show me the basics, like how to make the window, how to make buttons do things, input and output boxes, and stuff like that. I would prefer not to use pyQT or something like that.
The tkinter module is probably the most common Python GUI method.
To make a button:
from tkinter import *
tk = Tk()
btn = Button(tk, text="a clickable button", command())
btn.pack()
To make an input box:
from tkinter import *
tk = Tk()
inputBox = Entry(tk, bd=5)
#to read your box
inputBox.get()
To make a label:
from tkinter import *
tk = Tk()
label = Label(tk, text="your text here")

How to open a new window by clicking a button using Tkinter in python? [duplicate]

This question already has answers here:
How can I open a new window when the user clicks the button?
(2 answers)
Closed 6 years ago.
I want to make a gui application in python for that I was making this type of code.
I have already tired many codes but i was not able to make it up to the requirement.
What's stopping you from doing it, please refer the original post here. But basic code:
import Tkinter as tk
def create_window():
window = tk.Toplevel(root)
root = tk.Tk()
b = tk.Button(root, text="Create new window", command=create_window)
b.pack()
root.mainloop()

Categories

Resources