How to make an image in button? - python

I'm newbie in this site, but I have a question.
This is my code in Tkinter. My problem is that when I go to put an image inside a button, it does not display it.
Only in this program, because if I write the same things in a new clean file, it works. What's the problem ?
def newWindow():
root.destroy() #this destroy the main window
window = Tk()
window.title("Provisory title")
window.resizable(False,False)
window.geometry("420x430")
#BUTTON
bt3 = Button(window)
#BUTTON POSITIONING
bt3.place(x = 10, y = 10)
#image button
calc = PhotoImage(file= "icon_Calculator.png")
bt3.config(image = calc, width="100",height="100")

Related

How to close the Tkinter window when the mouse clicked away from the Tkinter window?

Good days all, I am new here, and I have stuck the problem a few days a problem currently with Tkinter, I have done some research about how to close Tkinter window when the mouse has clicked away from it but there are not much information to do so.
So, my problem is how to close the Tkinter window when the mouse clicked outside the Tkinter? I have tried the method of FocusOut to my Tkinter. However, I have tried to bind with root, it will close the window even though I clicked inside the frame widget. Then, I bind with the frame, the Tkinter will close when I clicked outside the Tkinter. Therefore, I have proved that the idea to close the Tkinter is works so far.
Then a new problem has happened, when I clicked the Combobox widget in the window, the window will close also. Is there any better solution to prove this concept?
Here is the code to indicate my problem.
import tkinter as tk
from tkinter import StringVar, ttk,messagebox
root = tk.Tk()
root.title("Sample Window")
root.minsize(300,350)
info_frame = tk.LabelFrame(root, text = "Information")
info_frame.pack(padx = 5, pady = 5 , fill = "both",expand=True)
tabControl = ttk.Notebook(info_frame)
person1tab = ttk.Frame(tabControl)
tabControl.add(person1tab,text = "Person1")
tabControl.pack(expand=1,fill="both")
person2tab = ttk.Frame(tabControl)
tabControl.add(person2tab,text = "Person2")
tabControl.pack(expand=1,fill="both")
fname_var = tk.StringVar()
lname_var = tk.StringVar()
gender_var = tk.StringVar()
age_var = tk.IntVar()
fname_label = tk.Label(person1tab, text = "First name:").pack(padx=5,pady=3)
fname_entry = tk.Entry(person1tab, textvariable=fname_var).pack(padx=5,pady=3)
lname_label = tk.Label(person1tab, text = "Last name:").pack(padx=5,pady=3)
lname_entry = tk.Entry(person1tab, textvariable=lname_var).pack(padx=5,pady=3)
gender_label = tk.Label(person1tab, text = "Gender:").pack(padx=5,pady=3)
gender_combo = ttk.Combobox(person1tab, textvariable=gender_var,state='readonly')
gender_combo['values'] = ('Male','Female')
gender_combo.current(0)
gender_combo.pack(padx=5,pady=3)
age_label = tk.Label(person1tab, text = "Age:").pack(padx=5,pady=3)
age_label = tk.Entry(person1tab, textvariable=age_var).pack(padx=5,pady=3)
page2label = tk.Label(person2tab,text = "This is tab 2.").pack(padx=5,pady=3)
def lossfocus(event):
root.quit()
pass
tabControl.bind('<FocusOut>', lossfocus)
root.mainloop()
You can still bind <FocusOut> on root window, but you need to check:
whether the widget that trigger this event is root window
no other widget in this root window getting the focus:
def lossfocus(event):
if event.widget is root:
# check which widget getting the focus
w = root.tk.call('focus')
if not w:
# not widget in this window
root.destroy()

How to add a button under an image in tkinter

I have googled about this problem but all i could find is how to add an image to a button. This is the opposite of my question kinda.
I have tried to use the image as a frame but did not work.
I have an image in mainwindow and i would like to add a button under the image. When i apply this the image disappears and i see only a little button somewhere on the screen. I add the button in the header method and below i share the picture with button and without button. Please ignore ugliness of the images.
class MainMenu():
def __init__(self,root,UserName):
self.root = root
self.root.geometry("800x500")
self.root.configure(background = "grey")
self.Body()
self.Header(UserName)
self.root.mainloop()
def Body(self):
MidImg = ImageTk.PhotoImage(Image.open("MyTkinter/Animasyon Çember/don1.png").resize((450,300)))
#MidImg = ImageTk.PhotoImage(Image.open("MyTkinter/images/aot1.jpg").resize((450,300)))
labelImg = Label(self.root,image = MidImg)
labelImg.image = MidImg
#labelImg.pack(side = "top",fill = "both", expand = "yes")
labelImg.grid(columnspan = 2,sticky = "N",padx = 180)
but= Button(labelImg, text = "Para Ekle")
but.grid(sticky = "SE")
Without button
With button

How do i pause my code before displaying the label

In my code, I am trying to make a loading screen for a frogger game but for some reason I am encountering a problem where I display a picture and then do the .sleep function before displaying a label over the top of it however it displays both of them at the same time it just runs the code 1 second after it should, can anyone help?
Here is my code below:
from tkinter import *
import tkinter as tk
import time
window = Tk()
window.geometry("1300x899")
LoadingScreen = PhotoImage(file = "FroggerLoad.gif")
Loading = Label(master = window, image = LoadingScreen)
Loading.pack()
Loading.place(x = 65, y = 0)
time.sleep(1)
FroggerDisplay = Label(master = window, font ("ComicSans",100,"bold"),text = "Frogger")
FroggerDisplay.pack()
FroggerDisplay.place(x = 500, y = 300)
window.mainloop()
When you use time.sleep(1) before starting the window.mainloop(), the window is created only after 1 second, and the FroggerDisplay label will be created at the same time with it. So, you can't use time.sleep(seconds) now.However, you can use window.after(ms, func) method, and place into the function all the code between time.sleep(1) and window.mainloop(). Note, that unlike the time.sleep(seconds) you must give the time to window.after (the first argument) as milliseconds.Here is the edited code:
from tkinter import *
def create_fd_label():
frogger_display = Label(root, font=("ComicSans", 100, "bold"), text="Frogger") # create a label to display
frogger_display.place(x=500, y=300) # place the label for frogger display
root = Tk() # create the root window
root.geometry("1300x899") # set the root window's size
loading_screen = PhotoImage(file="FroggerLoad.gif") # create the "Loading" image
loading = Label(root, image=loading_screen) # create the label with the "Loading" image
loading.place(x=65, y=0) # place the label for loading screen
root.after(1000, create_fd_label) # root.after(ms, func)
root.mainloop() # start the root window's mainloop
PS: 1) Why do you use .pack(...) and then .place(...) methods at the same time - the first one (.pack(...) here) will be ignored by Tkinter.
2) It's better to use a Canvas widget for creating a game - unlike labels it supports transparency and simpler to use. For example:
from tkinter import *
root = Tk() # create the root window
root.geometry("1300x899") # set the root window's size
canv = Canvas(root) # create the Canvas widget
canv.pack(fill=BOTH, expand=YES) # and pack it on the screen
loading_screen = PhotoImage(file="FroggerLoad.gif") # open the "Loading" image
canv.create_image((65, 0), image=loading_screen) # create it on the Canvas
root.after(1000, lambda: canv.create_text((500, 300),
font=("ComicSans", 100, "bold"),
text="Frogger")) # root.after(ms, func)
root.mainloop() # start the root window's mainloop
Note: you might need to change coords with Canvas.

Adding Windows to Buttons on Tkinter GUI

I'm new to Python, I'm trying to add widgets in an window which can be used when we click on an button when in Tkinter GUI.
I'm unable to add an window into the GUI button and I'm doubtful about the Code which can be Implemented as well. I hope I could get some inputs on this.
I'm running on IDLE 3.6.3.I would be grateful if someone could point out the additions that could be made and the changes in the current code.
ConnectLogo=PhotoImage(file="Connect.png")
Connect = Button(win,image=ConnectLogo,text = "Connect", font = myFont,height =100 , width = 100,compound=TOP,bg = "orange")
Connect.grid(row=3,column=1,padx=50,pady=40)
FrequencyLogo=PhotoImage(file="Frequency.png")
Frequency = Button(win,image=FrequencyLogo, text = "Frequency", font = myFont, height = 100, width =180,compound=TOP,bg = "Yellow")
Frequency.grid(row=3,column=2,padx=10)
MaskLogo=PhotoImage(file="Mask.gif")
Mask = Button(win,image=MaskLogo, text = "Mask", font = myFont, height = 100, width =180,compound=TOP,bg = "yellow")
Mask.grid(row=6,column=2,padx=10)
You can make a function, which will implement TopLevel.
This creates a new window into which you can add widgets, add them inside the function.Inside the function you root becomes window
from tkinter import *
root = Tk()
def new_window():
window = TopLevel(root)
...widgets like label, entry etc
label = Label(window,....)
btn = Button(...., command = new_window)
btn.pack()...(anything)

tkinter give window focus

I have a tkinter python 2.x program. I have several windows appearing on the screen. I use buttons to navigate from one to another, but I'm struggling to close a window and refocus on a currently opened window. I can open new windows just fine!
#code for main window
def frmMain():
app = Tk()
app.title("TWS: XML Options")
app.geometry("200x100")
bn1 = Button(app,text="Add", command=frmAdd)
bn1.grid(row = 2,column = 2, stick = W)
bn2 = Button(app,text="Edit", command=frmEdit)
bn2.grid(row = 2,column = 3, stick = W)
bn3 = Button(app,text="Delete", command=frmDelete)
bn3.grid(row = 2,column = 4, stick = W)
bn4 = Button(app,text="Back",command=frmMenu)
bn4.grid(row = 3,column = 2, stick = W)
app.mainloop()
#code for button on sub window
....
bn1 = Button(app,text="Back", command=back)
...
def back():
#Code to close current window and reopen frmMain
Just call destroy() on widget's parent.
bn1 = Button(app, text="Back", command=app.destroy)
I suppose you are not destroying the parent window, so once the Toplevel is destroyed, the focus is automatically returned to the previous opened window.
A TopLevel Window can be activated using its deiconify() method.
Use the basic Widget method focus_set() to set keyboard focus to a specific widget.

Categories