(Tkinter) Fill the gap between two background images, label border - python

I made a GUI for a Calendar. The blue background image has a grey border where the next image follows it.
I set the background image as Labels. So it seems like a label border.
The top and the Bottom of the image has a grey border. I suppose the grey border isn't on the sides because the image’s full width isn't accommodated to the application.
A picture here,
https://i.stack.imgur.com/73niQ.jpg
How do I get rid of the grey border where the two images connect?
Here’s the code,
import tkinter as tk
from tkinter import ttk
from tkinter import *
import calendar
from PIL import ImageTk, Image
screen = tk.Tk()
screen.geometry("1430x817")
title = screen.title("Class Calendar")
# Icon
img = tk.Image('photo', file = 'Calender_logo.png')
screen.tk.call('wm', 'iconphoto', screen._w, img)
# Scoll Bar
# main frame that holds everything.
main_frame = Frame(screen, bg = 'blue')
main_frame.pack(fill = BOTH, expand = 1)
# canvas that's getting scrolled
Canvasheight, Canvaswidth = 801, 1409.38
canvas = Canvas(main_frame, height = Canvasheight, width = Canvaswidth, borderwidth=0, highlightthickness =0, relief = 'flat', highlightcolor="white", highlightbackground="white")
canvas.pack(side = LEFT, fill= BOTH, expand = 1)
# Add a scrollbar to canvas
scroll_bar = ttk.Scrollbar( main_frame, orient=VERTICAL, command = canvas.yview)
scroll_bar.pack(side = RIGHT, fill= Y)
# Bind to mouse scrolling, configuring
canvas.configure(yscrollcommand=scroll_bar.set)
canvas.bind('<Configure>', lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
# Another frame inside the canvas # To hold the things on the canvas
second_frame = Frame(main_frame, height = Canvasheight*8, width = Canvaswidth, borderwidth=0, highlightthickness =0, relief= 'flat', highlightcolor="white", highlightbackground="white")
# Add new frame to a window in the canvas
canvas.create_window((0,0), window = second_frame, anchor='nw', width= Canvaswidth) # 'w' near middle lower half in scroll bar
second_frame.pack_propagate(False)
####################################### THE BACKGROUND IMAGES
img1 = ImageTk.PhotoImage(Image.open("Screenshot 2022-06-28 at 10.29.24 AM.png"))
label1 = Label(second_frame, image = img1, borderwidth=0, highlightthickness =0, relief= 'flat', highlightcolor="white", highlightbackground="white")
label1.pack()
img2 = ImageTk.PhotoImage(Image.open("Screenshot 2022-06-28 at 10.29.24 AM.png"))
label2 = Label(second_frame, image = img2, borderwidth=0, highlightthickness =0, relief= 'flat', highlightcolor="white", highlightbackground="white")
label2.pack()
# MARCH
# PRINTING MARCH TO WINDOW
march22_label = Label(second_frame, text = "MARCH", font = ("arial", 27),fg='black', borderwidth=0, highlightthickness =0)
march22_label.config(bg = '#9ad0f0')
march22_label.place( x= 660, y = 18)
# PRINTING WEEK TO WINDOW
marchweek22_label = Label(second_frame, text =
"""
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
""",
font= ('arial', 26),fg='black', borderwidth=0, highlightthickness =0)
marchweek22_label.config(bg = '#a1d0ea')
marchweek22_label.place(x= 100, y = 82)
def March22():
i = 1
x, y = 310, 194
while i < 7:
Button(second_frame, text = i, font = ("arial", 25), borderwidth=0, highlightthickness =0).place(x = x, y = y)
x = x + 185
i = i + 1
x, y = 125, y + 130
while i < 14:
Button(second_frame, text = i, font = ("arial", 25), borderwidth=0, highlightthickness =0).place(x = x, y = y)
x = x + 185
i = i + 1
x, y = 125, y + 130
while i < 21:
Button(second_frame, text = i, font = ("arial", 25),borderwidth=0, highlightthickness =0).place(x = x, y = y)
x = x + 185
i = i + 1
x, y = 125, y + 130
while i < 28:
Button(second_frame, text = i, font = ("arial", 25),borderwidth=0, highlightthickness =0).place(x = x, y = y)
x = x + 185
i = i + 1
x, y = 125, y + 130
while i < 32:
Button(second_frame, text = i, font = ("arial", 25),borderwidth=0, highlightthickness =0).place(x = x, y = y)
x = x + 185
i = i + 1
March22()
screen.mainloop()

Related

tkinter Enter and Leave Bindings

So the problem I am facing is with the "Enter" and "Leave" bind things. It works for hovering which is great but it also fire when the button is clicked which messes up my system which I have.
My system is that when being hovered over the images move up to show they are being highlighted and then when you click them they are selected.
The trouble is that when you click them it fires the "Enter" bind which causes the whole system to mess up. This is not ideal and is even more strange since I have already set the buttons "command".
Scouting for some solutions to this troubling issue thanks!
from tkinter import *
Activated = None
def command(self):
global Activated
if not Activated:
Activated = self
else:
Activated = None
Leave(self)
return
def Enter(self, *args):
global Activated
if not Activated:
width = self.winfo_width()
x = self.winfo_x()
y = self.winfo_y() - 1 / 2 * width
self.place(x = x, y = y)
return
def Leave(self, *args):
global Activated
if not Activated:
width = self.winfo_width()
x = self.winfo_x()
y = self.winfo_y() + 1 / 2 * width
self.place(x = x, y = y)
return
window = Tk()
window.geometry("1024x768")
window.configure(bg = "#1e1e1e")
canvas = Canvas(
window,
bg = "#1e1e1e",
height = 768,
width = 1024,
bd = 0,
highlightthickness = 0,
relief = "ridge")
canvas.place(x = 0, y = 0)
Rockimg = PhotoImage(file = f"Rock.png")
Rock = Button(
command = lambda: command(Rock),
bg = "#1e1e1e",
image = Rockimg,
borderwidth = 0,
highlightthickness = 0,
activebackground = "#1e1e1e",
relief = "flat")
Rock.bind("<Enter>", lambda event: Enter(Rock))
Rock.bind("<Leave>", lambda event: Leave(Rock))
Rock.place(
x = 46, y = 441,
width = 286,
height = 490.5)
Paperimg = PhotoImage(file = f"Paper.png")
Paper = Button(
command = lambda: command(Paper),
bg = "#1e1e1e",
image = Paperimg,
borderwidth = 0,
highlightthickness = 0,
activebackground = "#1e1e1e",
relief = "flat")
Paper.bind("<Enter>", lambda event: Enter(Paper))
Paper.bind("<Leave>", lambda event: Leave(Paper))
Paper.place(
x = 363, y = 316,
width = 297,
height = 678)
Scissorsimg = PhotoImage(file = f"Scissors.png")
Scissors = Button(
command = lambda: command(Scissors),
bg = "#1e1e1e",
image = Scissorsimg,
borderwidth = 0,
highlightthickness = 0,
activebackground = "#1e1e1e",
relief = "flat")
Scissors.bind("<Enter>", lambda event: Enter(Scissors))
Scissors.bind("<Leave>", lambda event: Leave(Scissors))
Scissors.place(
x = 721, y = 300,
width = 256,
height = 702)
window.resizable(False, False)
window.mainloop()
If I reproduced your problem correctly, I guess it should be something like this:
You hover over a random image and it goes up when your mouse is on it and goes down when you leave it
When you click the image and leave the image doesn't go down which messes up things later
You might be thinking its the problem with the <Enter> binding here because you think clicking it executed it again and when you left the button it stays up but that's not the problem. The problem is that the Activated variable is changed to self in the command function. So if you go to the Leave function you'll see that moving widget stuff is under an if statement. So now that the Activated variable is something that "stuff" does NOT get executed. So the step here is to remove those if statements. However there's another problem and I don't know why you did it, but you have called the Leave function under else in the command function. This will obviously make the widget go down twice. This code below works:
from tkinter import *
from PIL import Image, ImageTk
Activated = None
def command(self):
global Activated
if not Activated:
Activated = self
else:
Activated = None
return
def Enter(self, *args):
global Activated
width = self.winfo_width()
x = self.winfo_x()
y = self.winfo_y() - 1 / 2 * width
self.place(x = x, y = y)
return
def Leave(self, *args):
global Activated
width = self.winfo_width()
x = self.winfo_x()
y = self.winfo_y() + 1 / 2 * width
self.place(x = x, y = y)
return
window = Tk()
window.geometry("1024x768")
window.configure(bg = "#1e1e1e")
canvas = Canvas(
window,
bg = "#1e1e1e",
height = 768,
width = 1024,
bd = 0,
highlightthickness = 0,
relief = "ridge")
canvas.place(x = 0, y = 0)
Rockimg = Image.open(f"Rock.png")
Rockimg = Rockimg.resize((200, 200))
Rock = Button(
command = lambda: command(Rock),
bg = "#1e1e1e",
image = ImageTk.PhotoImage(Rockimg),
borderwidth = 0,
highlightthickness = 0,
activebackground = "#1e1e1e",
relief = "flat")
Rock.bind("<Enter>", lambda event: Enter(Rock))
Rock.bind("<Leave>", lambda event: Leave(Rock))
Rock.place(
x = 46, y = 441,
width = 286,
height = 490.5)
Paperimg = PhotoImage(file = f"Paper.png")
Paper = Button(
command = lambda: command(Paper),
bg = "#1e1e1e",
image = Paperimg,
borderwidth = 0,
highlightthickness = 0,
activebackground = "#1e1e1e",
relief = "flat")
Paper.bind("<Enter>", lambda event: Enter(Paper))
Paper.bind("<Leave>", lambda event: Leave(Paper))
Paper.place(
x = 363, y = 316,
width = 297,
height = 678)
Scissorsimg = PhotoImage(file = f"Scissors.png")
Scissors = Button(
command = lambda: command(Scissors),
bg = "#1e1e1e",
image = Scissorsimg,
borderwidth = 0,
highlightthickness = 0,
activebackground = "#1e1e1e",
relief = "flat")
Scissors.bind("<Enter>", lambda event: Enter(Scissors))
Scissors.bind("<Leave>", lambda event: Leave(Scissors))
Scissors.place(
x = 721, y = 300,
width = 256,
height = 702)
#window.resizable(False, False)
window.mainloop()

can you insert a canvas inside a frame in tkinter? [duplicate]

This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 6 months ago.
I'm trying to display a frame with a canvas inside in order I'll be able to change of frame with a button, but when the code is executed it does not even display the button, I don't know if it happens because of all is on a function, if doing what I think is even possible or if I'm missing something
from tkinter import *
window = Tk()
window.geometry("1200x700")
window.configure(bg = "#ffffff")
def btn_clicked():
print("Button Clicked")
frame1 =Frame(window, width=1200, height=700)
frame1.grid(row=0, column=0)
def load_page():
frame1.tkraise()
frame1.pack_propagate(False)
canvas = Canvas(
frame1,
bg = "#263ff8",
height = 700,
width = 1200,
bd = 0,
highlightthickness = 0,
relief = "ridge")
canvas.place(x = 0, y = 0)
background_img = PhotoImage(file = f"background.png")
background = canvas.create_image(
601.0, 341.0,
image=background_img)
img0 = PhotoImage(file = f"img0.png")
boton = Button(
image = img0,
borderwidth = 0,
highlightthickness = 0,
command = btn_clicked,
relief = "flat")
boton.place(
x = 85, y = 71,
width = 430,
height = 99)
load_page()
window.resizable(False, False)
window.mainloop()
Rephrased the entire code and including image:
To see button display. I had to reduced height and width in Canvas. Also The button had to reduced. In line 21, I changed bd = 10. You can comment in , because I don't used PhotoImage. you can debug see in image.
from tkinter import *
window = Tk()
window.geometry("1200x700")
window.configure(bg = "#ffffff")
def btn_clicked():
print("Button Clicked")
frame1 =Frame(window, width=1200, height=700)
frame1.grid(row=0, column=0)
def load_page():
frame1.tkraise()
frame1.pack_propagate(False)
canvas = Canvas(
frame1,
bg = "#263ff8",
height = 100,
width = 200,
bd = 10,
highlightthickness = 0,
relief = "ridge")
canvas.place(x = 0, y = 0)
#background_img = PhotoImage(file = f"background.png")
#background = canvas.create_image(
#601.0, 341.0,
#image=background_img)
#img0 = PhotoImage(file = f"img0.png")
boton = Button(frame1,
#image = img0,
borderwidth = 0,
highlightthickness = 0,
command = btn_clicked,
relief = "flat")
boton.place(
x = 85, y = 71,
width = 30,
height =29)
load_page()
window.resizable(False, False)
window.mainloop()
Output result. you can see debug.

How to place a widget in a specific position relative to the button clicked. (Tkinter)

it's a code for a Calendar, recording the subjects for a specific day.
I want it to open a dropdown list of the subject list when the day is clicked. And when multiple subjects are selected, I want the selected strings to be displayed a little under the button that was clicked, on the screen.
PROBLEM,
This is the code so far, I can’t accustom it to open the list, just under the specific button clicked. it opens in the center, I want it to open just under the specific button that is clicked. (I don't use an Optionbox because I want to select multiple items at once and it's not quite going well.)
import tkinter as tk
from tkinter import ttk
from tkinter import *
from PIL import ImageTk, Image
screen = tk.Tk()
screen.geometry("1423x817")
title = screen.title("Class Calendar")
# SCROLL BAR
# main frame that holds everything.
main_frame = Frame(screen, bg = 'blue')
main_frame.pack(fill = BOTH, expand = 1)
# CANVAS THAT IS SCROLLED
Canvasheight, Canvaswidth = 817, 1408
canvas = Canvas(main_frame, height = Canvasheight, width = Canvaswidth, borderwidth=0,
highlightthickness =0, relief = 'flat', highlightcolor="white", highlightbackground= 'white')
canvas.config(bg = '#a1d0ea')
canvas.pack(side = LEFT, fill= BOTH, expand = 1)
# ADDING SCROLLBAR TO CANVAS
scroll_bar = ttk.Scrollbar( main_frame, orient=VERTICAL, command = canvas.yview)
scroll_bar.pack(side = RIGHT, fill= Y)
# BIND TO MOUSE SCROLLING, CONFIGURING
canvas.configure(yscrollcommand=scroll_bar.set)
canvas.bind('<Configure>', lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
# A FRAME TO HOLD THINGS ON THE CANVAS
second_frame = Frame(main_frame, height = Canvasheight*8, width = Canvaswidth+5, borderwidth=0, highlightthickness =0, relief= 'flat', highlightcolor="white", highlightbackground='white')
second_frame.config(bg = '#a1d0ea')
canvas.create_window((0,0), window = second_frame, anchor='nw', width= Canvaswidth) # 'w' near middle lower half in scroll bar
# BACKGROUND PICTURE
img1 = ImageTk.PhotoImage(Image.open("Screenshot 2022-06-28 at 10.29.24 AM.png"))
label1 = Label(second_frame, image = img1, borderwidth=0, highlightthickness =0, relief= 'flat', highlightcolor="white", highlightbackground="white")
label1.config( bg = '#a1d0ea')
label1.pack()
img2 = ImageTk.PhotoImage(Image.open("Screenshot 2022-06-28 at 10.29.24 AM.png"))
label2 = Label(second_frame, image = img2, borderwidth=0, highlightthickness =0, relief= 'flat', highlightcolor="white", highlightbackground="white")
label2.config(bg = '#a1d0ea')
label2.pack()
# A SIDE MENUE
def toggle_win():
bg1 = ("#262626")
f3 = Frame(second_frame, width = 340, height = 817, bg = bg1)
f3.place(x = 0, y =0)
def dele():
f3.destroy()
menubtnclose = Button(f3, text = 'X', command = dele, border = 0, activebackground = '#12c4c0', borderwidth=0, highlightthickness =0).place(x = 5, y = 10)
anotherbtn = Button(second_frame, command = toggle_win, text = '=', font = (39), borderwidth=0, highlightthickness =0, bg = '#9ad0f0', highlightbackground='#9ad0f0', highlightcolor='#9ad0f0').place(x= 5, y = 10)
###################################################################
# THE OPTIONS LIST BOX
def optionbox():
#frame for list
global f4
f4 = Frame(second_frame, width = 200, height = 270, border = 1)
f4.place(x = 570, y = 260)
# func for closing
def delet():
def select():
var = StringVar
subject = []
sname = list2.curselection()
for i in sname:
op = list2.get(i)
subject.append(op)
for i in subject:
Label(second_frame, text = subject, borderwidth=0, highlightthickness =0, relief="flat", highlightcolor="white").place(x = newx, y = newy)
select()
f4.destroy()
fclose = Button(f4, text = 'X', command = delet, border = 0, activebackground = '#12c4c0', borderwidth=0, highlightthickness =0).place(x = 1, y = 1)
#listbox
global clicked
global sublist
clicked = StringVar()
global list2
list2 = Listbox(f4, font = ("arial", 20), width = 16, height = 9, selectborderwidth=1, selectmode=MULTIPLE)
list2.place(x = 0, y = 26)
# list
sublist = ["Biology", "Physics", "chemistry", "Science", "Sinhala"]
#insert items
for item in sublist:
list2.insert(0, item)
###############################################################
# CALENDAR
# PRINTING MARCH TO WINDOW
march22_label = Label(second_frame, text = "MARCH", font = ("arial", 27),fg='black', borderwidth=0, highlightthickness =0)
march22_label.config(bg = '#9ad0f0')
march22_label.place( x= 664, y = 18)
# PRINTING WEEK TO WINDOW
marchweek22_label = Label(second_frame, text =
"""
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
""",
font= ('arial', 25),fg='black', borderwidth=0, highlightthickness =0)
marchweek22_label.config(bg = '#a1d0ea')
marchweek22_label.place(x= 100, y = 82)
def March22():
i = 1
global newy
global newx
x, y = 310, 194
while i < 7:
newx, newy = x, y + 30
Button(second_frame, text = i, font = ("arial", 25), borderwidth=0, highlightthickness =0, command = optionbox).place(x = x, y = y)
x = x + 185
i = i + 1
x, y = 125, y + 130
while i < 14:
newx, newy = x, y + 30
Button(second_frame, text = i, font = ("arial", 25), borderwidth=0, highlightthickness =0, command = optionbox).place(x = x, y = y)
x = x + 185
i = i + 1
x, y = 125, y + 130
while i < 21:
newx, newy = x, y + 30
Button(second_frame, text = i, font = ("arial", 25),borderwidth=0, highlightthickness =0, command = optionbox).place(x = x, y = y)
x = x + 185
i = i + 1
x, y = 125, y + 130
while i < 28:
newx, newy = x, y + 30
Button(second_frame, text = i, font = ('arial', 25),borderwidth=0, highlightthickness =0, command = optionbox).place(x = x, y = y)
x = x + 185
i = i + 1
x, y = 125, y + 130
while i < 32:
newx, newy = x, y + 30
Button(second_frame, text = i, font = ("arial", 25),borderwidth=0, highlightthickness =0, command = optionbox).place(x = x, y = y)
x = x + 185
i = i + 1
March22()
screen.mainloop()
PROBLEM,
And when another button is clicked, without closing the previous list. it seems to just open on it and not open a completely different list, so it gets stuck and the Destroy command doesn't work.
PROBLEM,
And about displaying the selected subjects, I want it to be displayed under the specific button that was clicked, but I can't get that to work.
I’m quite new to python and Tkinter. I have tried a few ways to make it work, but I am not finding any successful results. Please help me in writing it. Please ask if I anything is unclear.
Instead of using a function to make the option box, it would be better to use a class which subclasses Frame.
class OptionBox(Frame):
def __init__(self, master, parent_x, parent_y):
# Initialise frame and place it
Frame.__init__(self, master, width = 200, height = 270, border = 1)
self.place(x=parent_x, y=parent_y)
# Store position of parent
self.parent_x = parent_x
self.parent_y = parent_y
fclose = Button(self, text = 'X', command = self.delete, border = 0, activebackground = '#12c4c0', borderwidth=0, highlightthickness =0).place(x = 1, y = 1)
self.clicked = StringVar()
self.list2 = Listbox(self, font = ("arial", 20), width = 16, height = 9, selectborderwidth=1, selectmode=MULTIPLE)
self.list2.place(x = 0, y = 26)
# list
self.sublist = ["Biology", "Physics", "chemistry", "Science", "Sinhala"]
#insert items
for item in self.sublist:
self.list2.insert(0, item)
def delete(self):
self.select()
self.destroy()
def select(self):
var = StringVar()
subject = []
sname = self.list2.curselection()
for i in sname:
op = self.list2.get(i)
subject.append(op)
for n, i in enumerate(subject):
Label(second_frame, text = i, borderwidth=0, highlightthickness =0, relief="flat", highlightcolor="white").place(x = self.parent_x, y = self.parent_y + (n*20))
This is similar to what you had before, but can be created multiple times without breaking the others. If you haven't seen classes before, research object oriented programming in python. This class uses the position of it's parent (the button which was clicked) to work out where to place itself and the subject labels.
There is also a simpler way to create the buttons for each day.
def March22():
x, y = 310, 194
# For each day in the month
for i in range(1, 32):
# Create a button
b = Button(second_frame, text = i, font = ("arial", 25), borderwidth=0, highlightthickness =0)
# Place it
b.place(x = x, y = y)
# Update the frame so we can get the height/width of the button
second_frame.update_idletasks()
# Create the option box command, increasing y by the height of the button so it is below it.
b.config(command = lambda x=x, y=y+b.winfo_height(): OptionBox(second_frame, x, y))
x += 185 # Increase x by 185 for every 1 across
# Start a new line every time i is 1 less than a multiple of 7
if i % 7 == 6:
x = 125 # Reset x
y += 130 # Increase y
Instead of using multiple while loops you can refactor it into a single for loop. The button command creates a new instance of OptionBox relative to the position of the button.

I CAN'T CENTER 2 FRAMES WITH TKINTER

I am writing again because I have enough problems with grid to center 2 frames. before with .pack it was easier for me but now with grid I can't center my 2 frames in my window, what am I doing wrong??
self.wind = window
self.wind.title('Aplicación')
screen_width=self.wind.winfo_screenwidth()
screen_height=self.wind.winfo_screenheight()
x= (screen_width / 2) - (Product.WIDTH /2)
y= (screen_height / 2) - (Product.HEIGHT /2)
self.wind.geometry(f"{Product.WIDTH}x{Product.HEIGHT}+{int(x)}+{int(y)}")
self.wind.resizable(False, False)
frame2 = customtkinter.CTkFrame(self.wind, corner_radius=5)
#frame2.pack(expand="true")
frame2.grid(row=0, sticky="NSEW",pady=(20,20))
self.label_hora = Label(frame2, font = ('calibri', 25, 'bold'),background = 'purple', foreground = 'white')
#self.label_hora.pack(fill=BOTH, expand=1)
self.label_hora.grid(stick="NSEW")
self.tick()
#frame principal
frame = customtkinter.CTkFrame(self.wind, corner_radius=5)
#frame.pack(expand="true")
frame.grid(row=1)
button_llegue = customtkinter.CTkButton(master=frame, corner_radius=8,fg_color="IndianRed3",hover_color="IndianRed4", text='Llegue',command=self.add_fecha_llegue)
button_llegue.grid(row = 0, column = 0,pady=(20,10),padx=(15,15))
button_sali = customtkinter.CTkButton(master=frame, corner_radius=8,fg_color="PaleGreen3",hover_color="PaleGreen4", text='Me Voy',command=self.add_fecha_sali)
button_sali.grid(row = 0, column = 1,pady=(20,10),padx=(15,15))
self.tree = ttk.Treeview(master=frame,height = 10, selectmode="browse")
self.tree.grid(row = 1,column=0,pady=(10,20),columnspan=2,sticky="NWSE",padx=(15,15))
self.tree.heading('#0', text = 'Ultimos registros', anchor = CENTER)
scrollbar = ttk.Scrollbar(frame,orient='vertical',command=self.tree.yview)
scrollbar.grid(row = 1, column=0, sticky="NSE",pady=(10,20),columnspan=2,padx=(0,15))
self.tree.config(yscrollcommand=scrollbar.set)
self.get_fechas()

How to make a button appear after a button click in tkinter?

I have this issue here that i want a button (b1 in code) to appear after the user click in the main button (b0).
from tkinter import *
from tkinter import filedialog
import tkinter.messagebox
import os
def openword():
my_program = filedialog.askopenfilename()
os.system('"%s"' % my_program)
def btn_clicked():
tkinter.messagebox.showinfo("Login","Login Success, Welcome!")
window = Tk()
window.geometry("1000x600")
window.configure(bg = "#293335")
canvas = Canvas(
window,
bg = "#293335",
height = 600,
width = 1000,
bd = 0,
highlightthickness = 0,
relief = "ridge")
canvas.place(x = 0, y = 0)
background_img = PhotoImage(file = f"background.png")
background = canvas.create_image(
508.5, 228.0,
image=background_img)
entry0_img = PhotoImage(file = f"img_textBox0.png")
entry0_bg = canvas.create_image(
166.0, 367.0,
image = entry0_img)
entry0 = Entry(
bd = 0,
bg = "#ffffff",
highlightthickness = 0)
entry0.place(
x = 22, y = 351,
width = 288,
height = 30)
entry1_img = PhotoImage(file = f"img_textBox1.png")
entry1_bg = canvas.create_image(
166.0, 456.0,
image = entry1_img)
entry1 = Entry(
bd = 0,
bg = "#ffffff",
highlightthickness = 0)
entry1.place(
x = 22, y = 440,
width = 288,
height = 30)
img0 = PhotoImage(file = f"img0.png")
b0 = Button(
image = img0,
borderwidth = 0,
highlightthickness = 0,
command = btn_clicked,
relief = "flat")
b0.place(
x = 28, y = 500,
width = 102,
height = 38)
img1 = PhotoImage(file = f"img1.png")
b1 = Button(
image = img1,
borderwidth = 0,
highlightthickness = 0,
command = openword,
relief = "flat")
b1.place(
x = 766, y = 505,
width = 213,
height = 72)
window.resizable(False, False)
window.mainloop()
The code works perfectly but the two buttons appear at the same time.
I need b1 to appear after the user presss b0.
Like acw1668 is suggesting, place your button inside your function that is called on clicking the other button. Here is an example:
import tkinter as tk
import tkinter.messagebox
def btn_clicked():
tkinter.messagebox.showinfo("Login","Login Success, Welcome!")
b2 = tk.Button(
borderwidth = 0,
highlightthickness = 0,
command = btn_clicked,
relief = "flat"
)
b2.place(
x = 200, y = 200,
width = 213,
height = 72
)
window = tk.Tk()
window.geometry("1000x600")
window.configure(bg = "#293335")
b1 = tk.Button(
borderwidth = 0,
highlightthickness = 0,
command = btn_clicked,
relief = "flat"
)
b1.place(
x = 5, y = 5,
width = 213,
height = 72
)
window.resizable(False, False)
window.mainloop()
You would want to place your button under the btn_clicked(): function. If you create a button under that function, I'm pretty sure that it creates a new button each time you click.

Categories