tkinter Enter and Leave Bindings - python

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()

Related

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.

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.

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)

Categories