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

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.

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.

Create event log list from tkinter button presses

This is my first venture into tkinter programming, so far I have the following code that increases or decreases a number by pressing either button. As you may notice I have started adding an update definition that I'd like to update a results table with the label value each time a button is pressed. I've recently found the lambda expression to add two commands to each button press but can't find an example to build the list:
import tkinter as tk
window = tk.Tk()
def increase():
value = int(lbl_value["text"])
lbl_value["text"] = f"{value + 1}"
def decrease():
value = int(lbl_value["text"])
lbl_value["text"] = f"{value - 1}"
def update():
result_table = []
window.rowconfigure(0, minsize = 100, weight = 1)
window.columnconfigure([0,1,2], minsize = 100, weight = 1)
btn_decrease = tk.Button(master = window, text = "-", command = lambda:[decrease(), update()], bg = 'red', fg = 'white')
btn_decrease.grid(row = 0, column = 0, sticky = "nsew")
lbl_value = tk.Label(master = window, text = "0")
lbl_value.grid(row = 0, column = 1)
btn_increase = tk.Button(master = window, text = "+", command = lambda:[increase(), update()], bg = 'green', fg = 'white')
btn_increase.grid(row = 0, column = 2, sticky = "nsew")
window.mainloop()
, bg = 'black', fg = 'white')
btn_decrease.grid(row = 0, column = 0, sticky = "nsew")
lbl_value = tk.Label(master = window, text = "0")
lbl_value.grid(row = 0, column = 1)
btn_increase = tk.Button(master = window, text = "+", command = increase, bg = 'red', fg = 'white')
btn_increase.grid(row = 0, column = 2, sticky = "nsew")
window.mainloop()
I'd like to add a graph of the count to the display ultimately. Any help greatly appreciated.
Matt

i am trying to store some data into a text file, there are no errors but it writes ".!toplevel.!entrywrite" instead of user input

i am making a sign up page and im trying to store the email the user entered to a text file but it doesnt seem to work it stores ".!toplevel.!entrywrite" instead of user input. i am new to this python and tkinter so i dont really know what to do, the code is a little bit long sorry about that.
Any help will be appreciated. Thank you
from tkinter import*
from PIL import Image, ImageTk
import tkinter as tk
root = Tk()
root.geometry('670x466')
class Goode_brothers:
def __init__(self, parent):
myFrame = Frame(parent)
myFrame.pack()
self.load = Image.open('new-dip-project\\food.jpg')
self.render = ImageTk.PhotoImage(self.load)
self.img = Label(parent, image = self.render)
self.img.place(x = -26, y =0)
self.img_login = PhotoImage(file = 'new-dip-project\\button (3).png')
self.b1 = Button(parent,image = self.img_login,bd = 0, bg = '#3b353b', activebackground = '#3b353b')
self.b1.place(x = 275, y = 310)
self.img_register = PhotoImage(file = 'new-dip-project\\register.png')
self.b2 = Button(parent,image = self.img_register, command = self.openNewWindow, bd = 0, bg = '#3b353b', activebackground = '#3b353b')
self.b2.place(x = 265, y = 400)
self.canvas = Canvas(parent, width = 400, height = 120)
self.canvas.pack()
self.img4 = ImageTk.PhotoImage(Image.open('new-dip-project\\goode.png'))
self.canvas.create_image(20, 20, anchor=NW, image=self.img4)
self.email = Entry(parent).place(x = 340, y = 180)
self.password = Entry(parent).place(x = 340, y = 250)
self.img_label = PhotoImage(file = 'new-dip-project\\label-image.png')
self.name = Label(parent, image = self.img_label, text = "Email:", bg = '#3c3a3b').place(x = 197,y = 178)
self.img_label_pass = PhotoImage(file = 'new-dip-project\\label_pass.png')
self.name = Label(parent, image = self.img_label_pass, text = "Password:", bg = '#3c3a3b').place(x = 177,y = 245)
def create_pass(self):
self.password_length = Label(self.root2, text = '')
self.password_length.place(x = 80, y = 140)
self.pass_word = str(self.password2.get()) #this code is getting the users input from the password entry box
if len(self.pass_word) >= 8: #if the characters gotten from the pasword entry is less than 8, an erorr message will appear
self.registered = Label(self.root2, text = 'You have successfully registered, this window will now automatically close', font=("open sans", "8"))
self.registered.place(x = 80, y = 140)
self.root2.after(4000, self.root2.destroy)
else:
self.password_length.configure(text="""Your password must be atleast eight characters long. Please try again""", font=("open sans", "8"))
def save_info(self):
self.email_reg = str(self.email2.get())
print(self.email2)
file = open('emails.txt', 'w')
file.write(str(self.email2))
def create_email(self):
self.username_length = Label(self.root2, text = '', font = '40')
self.username_length.place(x = 165, y = 140)
self.email_reg = str(self.email2.get())
if len(self.email_reg) >= 1: #if user has inputted a letter or number it will allow it to go to the next function
self.save_info()
self.create_pass()
self.username_length.destroy()
else:
self.username_length.configure(text='Please enter your username or password', font=("open sans", "8"))
self.username_length.after(3000, self.username_length.destroy)
def openNewWindow(self):
# Toplevel object which will
# be treated as a new window
self.root2 = Toplevel(root)
# sets the title of the
# Toplevel widget
self.root2.title("New Window")
# sets the geometry of toplevel
self.root2.geometry("500x300")
self.load2 = Image.open('new-dip-project\\registerscreen3.jpg')
self.render2 = ImageTk.PhotoImage(self.load2)
self.img2 = Label(self.root2, image = self.render2)
self.img2.place(x = -2, y =0)
self.img_label2 = PhotoImage(file = 'new-dip-project\\label-image.png')
self.name = Label(self.root2, image = self.img_label, bg = '#292929').place(x = 130,y = 102)
self.img_label_pass2 = PhotoImage(file = 'new-dip-project\\label_pass.png')
self.name = Label(self.root2, image = self.img_label_pass, bg = '#292929').place(x = 120,y = 173)
self.email2 = Entry(self.root2)
self.email2.place(x = 280, y = 104)
self.password2 = Entry(self.root2)
self.password2.place(x = 280, y = 180)
self.img_register2 = PhotoImage(file = 'new-dip-project\\register.png')
self.b3 = Button(self.root2,image = self.img_register2, command = self.create_email, bd = 0, bg = '#0d0d0d', activebackground = '#0d0d0d')
self.b3.place(x = 180, y = 250)
self.img_reg2 = PhotoImage(file = 'new-dip-project\\regtitle.png')
self.name9 = Label(self.root2, image = self.img_reg2, bg = '#131313')
self.name9.place(x = 109, y = 10)
if __name__ == "__main__":
e = Goode_brothers(root)
root.title('Goode brothers')
root.mainloop()
Using var = StringVar() to set option textvariable - var when initialize Entry, and get content of Entry by var.get().
example Code
from tkinter import *
def on_click():
print(entry_text.get())
root = Tk()
font = ("Courier New", 32)
entry_text = StringVar()
entry = Entry(root, textvariable=entry_text, font=font)
entry.place(x=0, y=0)
button = Button(root, text='Check', command=on_click, font= font)
button.place(x=0, y=64)
root.mainloop()
Following statement will get value None for self.email
self.email = Entry(parent).place(x = 340, y = 180)
should be
self.email = Entry(parent)
self.email.place(x = 340, y = 180)

Python tkinter combobox input selection based hide/show frame(labels and widgets packed inside frame)

from tkinter import *
from tkinter import ttk
import sqlite3
import os
import sys
import datetime
connection = sqlite3.connect("employees.db")
cursor = connection.cursor()
class Main(object):
def __init__(self, master):
self.master = master
#<-----Defining Frames----->
main_frame = Frame(self.master)
main_frame.pack()
top_frame = Frame(main_frame, width = 1250, height = 50, bg = '#f8f8f8', padx = 20, relief = SUNKEN, borderwidth = 1)
top_frame.pack(side = TOP, fill = X)
top_right_frame = Frame(top_frame, width = 100, height = 20, bg = '#f8f8f8', relief = SUNKEN, borderwidth = 0)
top_right_frame.pack(side = RIGHT)
center_frame = Frame(main_frame, width = 1350, height = 690, relief = RIDGE, bg = '#e6e6ff', borderwidth = 1)
center_frame.pack(side = TOP)
left_center_frame = Frame(center_frame, width = 180, height = 690, relief = SUNKEN, bg = '#e6e6ff', borderwidth = 1)
left_center_frame.pack(side = LEFT, fill = BOTH)
left_center_frame.pack_propagate(0)
right_center_frame = Frame(center_frame, width = 1170, height = 690, relief = SUNKEN, bg = '#e6e6ff', borderwidth = 1)
right_center_frame.pack()
right_center_frame.pack_propagate(0)
bottom_frame = Frame(main_frame, width = 1350, height = 10, relief = SUNKEN, bg = '#8080ff', borderwidth = 0)
bottom_frame.pack(side = BOTTOM, fill = X)
swap_type_frame = LabelFrame(left_center_frame, width = 250, height = 50, text = "Swap Type", bg = '#e6e6ff', padx = 5, pady = 5)
swap_type_frame.pack(side = TOP, fill = BOTH, padx = 5, pady = 5)
#<-----Defining Labels----->
label_main_title = Label(top_frame, text = "Advisor Roster Swap", bg = '#f8f8f8', font = ("TIMES",20), justify = CENTER)
label_main_title.pack()
label_current_day = Label(top_right_frame, text = timeDetails().current_date(), bg = '#f8f8f8', font = ("Times New Roman",10), anchor = NE)
label_current_day.pack(side = RIGHT)
label_bottom_frame = Label(bottom_frame, text = "© Vodafone WFM | RTA", font = ("Times New Roman",10), justify = CENTER, fg= '#ffffff', bg = '#8080ff' )
label_bottom_frame.pack(side = BOTTOM)
swap_type = ['One Way Swap', 'Two Way Swap', 'One Way CL Swap', 'Two Way CL Swap']
swap_type_combobox = ttk.Combobox(swap_type_frame, value = swap_type, width = 21)
swap_type_combobox.current(0)
swap_type_combobox.grid(row = 2, column = 1, pady = 5)
class Single_Advisor(object):
def __init__(self, master):
self.master = master
single_advisor_frame = LabelFrame(right_center_frame, width = 1150, height = 200, text = "Advisor Details", bg = '#e6e6ff', padx = 5, pady = 5)
single_advisor_frame.pack(side=TOP, fill=Y, padx = 5, pady = 5)
class Multiple_Advisors(object):
def __init__(self, master):
self.master = master
multiple_advisor_frame = LabelFrame(right_center_frame, width = 1150, height = 200, text = "Advisor Details", bg = '#e6e6ff', padx = 5, pady = 5)
multiple_advisor_frame.pack(side=TOP, fill=Y, padx = 5, pady = 5)
class timeDetails():
def current_date(self):
now = datetime.datetime.now()
return now.strftime("%A, %B %d")
def main():
root = Tk()
app = Main(root)
root.title("Advisor Roster Swap")
root.geometry("1350x750+350+200")
root.resizable(width = FALSE, height = FALSE)
root.mainloop()
if __name__ == '__main__':
main()
Hello, I want to hide/show frame and it's labels/widgets/boxes etc which will depend on the user selection in combo box. When selected, correct frame should appear which I have mentioned in class so that they can have one way swap or 2 way swap.
One way would have employee code boxes along with 7 boxes(shifts and offs) and 2 way will have 2 employee codes boxes along with 7X2 boxes for shifts and offs.

Categories