calling functions in one another in python - python

i am learning python and got stuck in a place where i want to call the first function in the second function and second function into first function on clicking a button in tkinter but due to sequencing in python i am not able to do this. plz ignore the ending part after functions.
from tkinter import *
log = Tk()
log.config(bg="grey")
log.resizable(False, False)
log.title("this is gui python")
log.geometry("800x500")
log.resizable(False, False)
frame = LabelFrame(log, bg="blue", width=200, height=500, padx=0, pady=0).place(x=0, y=0)
def sign_up():
global log
log.config(bg="grey")
log.resizable(False, False)
log.title("this is gui python")
log.geometry("800x500")
log.resizable(False, False)
frame = LabelFrame(log, bg="blue", width=200, height=500, padx=0, pady=0).place(x=0, y=0)
B1 = Button(frame, text="login window", width=27, bg="violet", pady=10, command=signin_window).place(x=1, y=0)
B2 = Button(frame, text="sign up window", width=27, bg="violet", pady=10).place(x=1, y=0)
def signin_window():
global log
log.config(bg="grey")
log.resizable(False, False)
log.title("this is gui python")
log.geometry("800x500")
log.resizable(False, False)
frame = LabelFrame(log, bg="blue", width=200, height=500, padx=0, pady=0).place(x=0, y=0)
B1 = Button(frame, text="login window", width=27, bg="violet", pady=10, relief=FLAT,).place(x=1, y=0)
B2 = Button(frame, text="sign up window", width=27, bg="violet", pady=10, relief=FLAT, command=sign_up).place(x=1, y=50)
B1 = Button(frame, text="login window", width=27, bg="violet", relief=FLAT, pady=10, command=signin_window).place(x=1, y=0)
B2 = Button(frame, text="sign up window", width=27, bg="violet", relief=FLAT, pady=10).place(x=1, y=50)
l1 = Label(log, text="welcome to login window", font="haventica 30 bold", bg="grey").place(x=250, y=110)
l2 = Label(log, text="email", font="haventica 20", bg="grey").place(x=280, y=170)
l3 = Label(log, text="password", font="haventica 20", bg="grey").place(x=280, y=220)
e1 = Entry(log, width=40, borderwidth=3)
e1.place(x=430, y=170)
e2 = Entry(log, width=40, borderwidth=3)
e2.place(x=430, y=220)
log.mainloop()

in this part in the sign_up() function you did this:
B1 = Button(frame, text="login window", width=27, bg="violet", pady=10, command=signin_window).place(x=1, y=0)
where you assigned the command "signin_window()" to it.
do you see? you used the command signin_window() before making it!
so just switch the 2 commands around and you are done.
hope it was helpful.

Related

Is there a way to create GUI component in Python Tkinter?

I am writing GUI in Python using Tkinter and I am having some difficulties. I have this repeatable segment:
It repeats 8 times.
I would like to create class or struct of:
frame1 = LabelFrame(root, text="Host 0", padx=5, pady=5)
frame1.grid(row=0, column=0)
labela1 = Label(frame1, text="ID 21")
c1 = Checkbutton(frame1, text="Save images")
c1.grid(row=2, column=1, columnspan=2)
b11 = Button(frame1, text="Start host")
b12 = Button(frame1, text="Start app")
b13 = Button(frame1, text="Kill app")
b14 = Button(frame1, text="Turn off host")
labela1.grid(row=1, column=0)
b11.grid(row=1, column=2)
b12.grid(row=1, column=3)
b13.grid(row=1, column=4)
b14.grid(row=1, column=5)
labela12 = Label(frame1, text="Status", fg='#00f')
labela12.grid(row=2, column=3, columnspan=4)
and then populate GUI through for loop.
Is this possible in python?
I really couldn't find anything on the web for this type of problem.
Thanks in advance!
The most common and easiest way of creating a custom widget is to start by creating a class that inherits from Frame or LabelFrame. Put anything you want inside that class. You can then use that class just like any other widget.
import tkinter as tk
class HostController(tk.LabelFrame):
def __init__(self, parent, hostid, title):
super().__init__(parent, text=title)
self.hostid = hostid
labela1 = tk.Label(self, text=f"ID {hostid}")
c1 = tk.Checkbutton(self, text="Save images")
c1.grid(row=2, column=1, columnspan=2)
b11 = tk.Button(self, text="Start host")
b12 = tk.Button(self, text="Start app")
b13 = tk.Button(self, text="Kill app")
b14 = tk.Button(self, text="Turn off host")
labela1.grid(row=1, column=0)
b11.grid(row=1, column=2)
b12.grid(row=1, column=3)
b13.grid(row=1, column=4)
b14.grid(row=1, column=5)
labela12 = tk.Label(self, text="Status", fg='#00f')
labela12.grid(row=2, column=3, columnspan=4)
root = tk.Tk()
for i in range(5):
hc = HostController(root, hostid=i, title=f"Host {i}")
hc.pack(side="top", padx=4, pady=(0,4))
root.mainloop()

it seems like i can't open a frame in python

i want to open the menu_frame by clicking the login_button but the frame won't come up and there are no error messages showing up. its my first time and im so lost
ive tried to google how to fix this problem but from what ive read, it seems to me that there are no errors or any reason for this code to not function properly. please help :(
from tkinter import *
window = Tk()
window.title("EL TALLO")
window.geometry("700x490")
window.config(background="#FFF8E5")
#회원가입
def register_frame():
register_frame = Frame(
window,
bd=2,
bg='#FFF8E5',
relief=SOLID,
padx=10,
pady=10
)
Label(
register_frame,
text="ID입력",
bg='#CCCCCC',
).grid(row=0, column=0, sticky=W, pady=10)
Label(
register_frame,
text="비밀번호 입력",
bg='#CCCCCC',
).grid(row=5, column=0, sticky=W, pady=10)
newlyset_id = Entry(
register_frame
)
newlyset_pw = Entry(
register_frame,
show='*'
)
register_btn = Button(
register_frame,
width=15,
text='회원가입',
relief=SOLID,
cursor='hand2',
command=register_frame.destroy
)
newlyset_id.grid(row=0, column=1, pady=10, padx=20)
newlyset_pw.grid(row=5, column=1, pady=10, padx=20)
register_btn.grid(row=7, column=1, pady=10, padx=20)
register_frame.pack()
register_frame.place(x=220, y=150)
def new_id(): #new_id에 newlyset_id에 입력한 값을 저장
new_id = newlyset_id.get()
def new_pw(): #new_pw에 newlyset_pw에 입력한 값을 저장
new_pw = newlyset_pw.get()
#메뉴화면
def menu_frame():
menu_frame = Frame(
window,
bd=2,
bg='#FFF8E5',
relief=SOLID,
padx=10,
pady=10
)
label1 = Label(menu_frame, text = "EL TALLO", bg="lightgreen",width=10, height=1, font=(15))
label1.pack()
btn1 = Button(menu_frame, text = "play game", bg="gray", width=15, height=1)
btn1.pack()
btn2 = Button(menu_frame, text = "How to play", bg="gray", width=15, height=1)
btn2.pack()
btn3 = Button(menu_frame, text = "Settings", bg="gray", width=15, height=1)
btn3.pack()
def btncmd():
print("게임이 종료되었습니다")
btn4 = Button(menu_frame, text = "END GAME", command=btncmd, bg="lightgreen", width=15, height=1)
btn4.pack()
label1.place(x=50, y=50)
btn1.place(x=50, y=100)
btn2.place(x=50, y=150)
btn3.place(x=50, y=200)
btn4.place(x=50, y=250)
#로그인
Label(
window,
text="아이디 입력",
bg='#CCCCCC',
).place(x=230, y=170)
id_tf = Entry(
window,
).place(x=330, y=170)
def id(): #id에 id_tf에 입력한 값을 저장
id = id_tf.get()
Label(
window,
text="비밀번호 입력",
bg='#CCCCCC',
).place(x=230, y=220)
pw_tf = Entry(
window,
).place(x=330, y=220)
def pw(): #pw에 pw_tf에 입력한 값을 저장
pw = pw_tf.get()
#회원가입 버튼
registerbutton = Button(
window,
width=15,
text="회원가입",
bg="#CCCCCC",
cursor='hand2',
command=register_frame
)
registerbutton.place(x=360, y=270)
#로그인 버튼
loginbutton = Button(
window,
width=15,
text="로그인",
bg="#CCCCCC",
cursor='hand2',
command=menu_frame
)
loginbutton.place(x=230, y=270)
window.mainloop()
You didn't pack the menu_frame and the indentation of def btncmd() was wrong.
That is:
btn3 = Button(menu_frame, text = "Settings", bg="gray", width=15, height=1)
btn3.pack()
menu_frame.pack()
def btncmd():
print("게임이 종료되었습니다")

How to center a frame with Tkinter?

I searched a lot about how to center a frame with Tkinter and Python 3.8.
I wrote a code where 4 buttons are in a frame but I don't know how to center the frame in the window. I tried several methods like grid_propagate(0), grid(sticky=""), pack(expand=True), ...
But nothing works then.
I am sharing my most recent code there. I hope you can help me.
window = Tk()
frame = Frame(window)
button1 = Button(frame, text="Button 1")
button2 = Button(frame, text="Button 2")
button3 = Button(frame, text="Button 3")
button4 = Button(frame, text="Button 4")
button1.grid(row=0, column=0)
button2.grid(row=0, column=1)
button3.grid(row=1, column=0)
button4.grid(row=1, column=1)
frame.grid_propagate(0)
frame.grid(row=0, column=0, rowspan=2, columnspan=2)
window.mainloop()
It works for me
Here is the code
from tkinter import *
window = Tk()
frame = Frame(window)
button1 = Button(frame, text="Button 1")
button2 = Button(frame, text="Button 2")
button3 = Button(frame, text="Button 3")
button4 = Button(frame, text="Button 4")
button1.grid(row=0, column=0)
button2.grid(row=0, column=1)
button3.grid(row=1, column=0)
button4.grid(row=1, column=1)
frame.place(relx=0.5,rely=0.5,anchor="c")
window.mainloop()

How to go back to previous frame after button click using Tkinter on Python?

I want to have a GUI with 2 buttons. Once clicked on either button, I want to see a new GUI which has a button to go back to the main GUI with the two buttons again.
This is what I've got right now but the 'Go back' button doesn't do anything. How can I go back to my first page using tkinter?
from tkinter import *
root = Tk()
root.title('Frames')
root.geometry('500x250+300+300')
# Position frame
frame = LabelFrame(root, text='Such a dilemma', padx=25, pady=25)
frame.pack(padx=10, pady=50)
# What do the buttons do
def bad():
frame.grid_forget()
b.grid_forget()
b2.grid_forget()
slechtekeuze = Label(frame, text='Bad choice')
slechtekeuze.grid(row=0, column=0, columnspan=2)
# Option to got back
homepage = Button(frame, text='Go back', command=back)
homepage.grid(row=1, column=0, columnspan=2, pady=10)
def good():
frame.grid_forget()
b.grid_forget()
b2.grid_forget()
slechtekeuze = Label(frame, text='Good choice')
slechtekeuze.grid(row=0, column=0, columnspan=2)
# Option to go back
homepage = Button(frame, text='Terug', command=back)
homepage.grid(row=1, column=0, columnspan=2, pady=10)
def back():
frame.grid_forget()
frame1 = LabelFrame(root, text='Such a dilemma', padx=25, pady=25)
frame1.pack(padx=10, pady=50)
b = Button(frame1, text="Don't click!!!", fg='red', command=bad)
b2 = Button(frame1, text='Click!!!', fg='green', command=good)
b.grid(row=0, column=0, padx=3)
b2.grid(row=0, column=1, padx=3)
# Create the buttons and put them in the frame
b = Button(frame, text="Don't click!!!", fg='red', command=bad)
b2 = Button(frame, text='Click!!!', fg='green', command=good)
b.grid(row=0, column=0, padx=3)
b2.grid(row=0, column=1, padx=3)
root.mainloop()
It does work, expand your screen a bit. The frames get added below.
I've edited it a bit as much in your style as possible:
from tkinter import *
root = Tk()
root.title('Frames')
root.geometry('500x250+300+300')
# Position frame
frame = LabelFrame(root, text='Such a dilemma', padx=25, pady=25)
frame.pack(padx=10, pady=50)
# What do the buttons do
def bad(frame):
frame.destroy()
frame = LabelFrame(root, text='Such a dilemma', padx=25, pady=25)
frame.pack(padx=10, pady=50)
slechtekeuze = Label(frame, text='Bad choice')
slechtekeuze.grid(row=0, column=0, columnspan=2)
# Option to got back
homepage = Button(frame, text='Go back', command=lambda:back(frame))
homepage.grid(row=1, column=0, columnspan=2, pady=10)
def good(frame):
frame.destroy()
frame = LabelFrame(root, text='Such a dilemma', padx=25, pady=25)
frame.pack(padx=10, pady=50)
slechtekeuze = Label(frame, text='Good choice')
slechtekeuze.grid(row=0, column=0, columnspan=2)
# Option to go back
homepage = Button(frame, text='Terug', command=lambda:back(frame))
homepage.grid(row=1, column=0, columnspan=2, pady=10)
def back(frame):
frame.destroy()
frame = LabelFrame(root, text='Such a dilemma', padx=25, pady=25)
frame.pack(padx=10, pady=50)
b = Button(frame, text="Don't click!!!", fg='red', command=lambda:bad(frame))
b2 = Button(frame, text='Click!!!', fg='green', command=lambda:good(frame))
b.grid(row=0, column=0, padx=3)
b2.grid(row=0, column=1, padx=3)
# Create the buttons and put them in the frame
b = Button(frame, text="Don't click!!!", fg='red', command=lambda:bad(frame))
b2 = Button(frame, text='Click!!!', fg='green', command=lambda:good(frame))
b.grid(row=0, column=0, padx=3)
b2.grid(row=0, column=1, padx=3)
root.mainloop()

Python Tkinter Entry() Calculations

I am currently trying to build a GUI calculator using python with the help of tkinter. I have managed to set up all of my buttons and they interact with my entry() bar when I press the button (e,g: Press button 5 and 5 appears into the entry()).
The only thing left to do is actually perform the mathematical equations that appear in the entry(). For example, if I enter 5 + 5 * 2 into the entry bar, how would I make the answer appear into the entry() after it's updated? So basically all I'm asking is someone to help me make this happen!
I have provided the code below and a screenshot of the calculator. Also please don't report this as a duplicate or flag this question as a post, (I'm reasking this question because I got no help on my last one so please help a young python programmer out!). Also I'm asking you not to provide me with links because I've read every possible link on tkinter there is to read and still have no clue what I'm doing.
Here is my code:
import sys
from tkinter import *
from PIL import Image, ImageTk
#ACTIONS:
def clear():
#Action: Clears the entry().
txtDisplay.delete(0,END);
return;
def update_Entry(inputValue):
#Updates the entry when a button is pressed.
currentText = num1.get();
update = num1.set(currentText + inputValue);
#Parent Window.
root = Tk();
root.title('Calculator ++ [1.7.2]');
root.geometry('350x450');
#Main entry.
num1 = StringVar();
txtDisplay = Entry(root, textvariable = num1, relief=RIDGE, bd = 10, width=33, insertwidth = 1, font = 40, justify=RIGHT);
txtDisplay.place(x=15, y=10);
txtDisplay.focus();
print(txtDisplay.get())
#Buttons:
zeroButton = Button(root, text='0', width=20, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('0'));
zeroButton.place(x=17,y=382);
oneButton = Button(root, text='1', width=8, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('1'));
oneButton.place(x=17, y=302);
twoButton = Button(root, text='2', width=8, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('2'));
twoButton.place(x=100, y=302);
threeButton = Button(root, text='3', width=8, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('3'));
threeButton.place(x=182, y=302);
fourButton = Button(root, text='4', width=8, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('4'));
fourButton.place(x=17, y=222);
fiveButton = Button(root, text='5', width=8, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('5'));
fiveButton.place(x=100, y=222);
sixButton = Button(root, text='6', width=8, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('6'));
sixButton.place(x=182, y=222);
sevenButton = Button(root, text='7', width=8, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('7'));
sevenButton.place(x=17, y=142);
eightButton = Button(root, text='8', width=8, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('8'));
eightButton.place(x=100, y=142);
ninthButton = Button(root, text='9', width=8, height=3, bg='LightBlue', fg='red', command=lambda:update_Entry('9'));
ninthButton.place(x=182, y=142);
decimalButton = Button(root, text='.', width=8, height=3, bg='powder blue', command=lambda:update_Entry('.'));
decimalButton.place(x=182, y=382);
equalButton = Button(root, text='=', width=8, height=8, bg='Lightgreen', command=lambda:update_Entry('='));
equalButton.place(x=264, y=307);
plusButton = Button(root, text='+', width=8, height=3, bg='gray', command=lambda:update_Entry('+'));
plusButton.place(x=264, y=222);
minusButton = Button(root, text='-', width=8, height=3, bg='gray', command=lambda:update_Entry('-'));
minusButton.place(x=264, y=142);
multiplyButton = Button(root, text='x', width=8, height=3, bg='gray', command=lambda:update_Entry('*'));
multiplyButton.place(x=264, y=66);
divideButton = Button(root, text='÷', width=8, height=3, bg='gray', command=lambda:update_Entry('/'));
divideButton.place(x=182, y=66);
clearButton = Button(root, text='Clear (CE)', width=20, height=3, command = clear, bg='Orange');
clearButton.place(x=17, y=66);
#Locks the parent windows size.
root.maxsize(350,450);
root.minsize(350,450);
#Parent window's background color:
root.configure(background = 'black');
root.mainloop();
Screenshot:
For something this simple, try having this as the command for your equalButton:
def evaluate():
currentText = num1.get()
try:
num1.set(str(eval(currentText)))
except SyntaxError:
num1.set('<ERROR>')

Categories