I am trying to build an atm software I'm still learning and I just need help
I can't figure out how to put a value in the entry box with a button
trying to put value 50 in the box like in the ATM when a button 50 is pressed ...
I'm pretty new and I know my code is not perfect ill take any help and criticism...
any advice for good practice?
import tkinter as tk
CreditCardAmount = float(1500)
DebitCardAmount = float(500)
def DB_50_click(D_E):
D_E.delete(0, tk.END)
D_E.insert(0, D_E)
def Deposit_BB():
deposit_window = tk.Tk()
deposit_window.resizable(False, False)
deposit_window.geometry("700x300")
D_L = tk.Label(deposit_window, text="Amount you would like to deposit :")
D_L.place(x=247, y=100)
D_E = tk.Entry(deposit_window, width=10, font=("Arial", 30))
D_E.place(x=250, y=125)
DB_50 = tk.Button(
deposit_window,
text="50",
width=20,
height=3,
command=lambda: DB_50_click("50"),
)
DB_50.place(x=0, y=30)
DB_100 = tk.Button(deposit_window, text="100", command="", width=20, height=3)
DB_100.place(x=0, y=80)
DB_150 = tk.Button(deposit_window, text="150", command="", width=20, height=3)
DB_150.place(x=0, y=130)
DB_200 = tk.Button(deposit_window, text="200", command="", width=20, height=3)
DB_200.place(x=0, y=180)
DB_250 = tk.Button(deposit_window, text="250", command="", width=20, height=3)
DB_250.place(x=550, y=30)
DB_250 = tk.Button(deposit_window, text="300", command="", width=20, height=3)
DB_250.place(x=550, y=80)
DB_250 = tk.Button(deposit_window, text="350", command="", width=20, height=3)
DB_250.place(x=550, y=130)
DB_250 = tk.Button(deposit_window, text="400", command="", width=20, height=3)
DB_250.place(x=550, y=180)
DB_250 = tk.Button(deposit_window, text="Cancle", command="", width=20, height=4)
DB_250.place(x=550, y=230)
Enter_DB = tk.Button(deposit_window, text="Enter", command="", width=20, height=4)
Enter_DB.place(x=0, y=230)
def Wintdrow_BB():
windraw_window = tk.Tk()
windraw_window.resizable(False, False)
windraw_window.geometry("700x300")
W_L = tk.Label(windraw_window, text="Amount you would like to draw :")
W_L.place(x=247, y=100)
W_E = tk.Entry(windraw_window, width=10, font=("Arial", 30))
W_E.place(x=250, y=125)
WB_50 = tk.Button(windraw_window, text="50", command="", width=20, height=3)
WB_50.place(x=0, y=30)
WB_100 = tk.Button(windraw_window, text="100", command="", width=20, height=3)
WB_100.place(x=0, y=80)
WB_150 = tk.Button(windraw_window, text="150", command="", width=20, height=3)
WB_150.place(x=0, y=130)
WB_200 = tk.Button(windraw_window, text="200", command="", width=20, height=3)
WB_200.place(x=0, y=180)
WB_250 = tk.Button(windraw_window, text="250", command="", width=20, height=3)
WB_250.place(x=550, y=30)
WB_250 = tk.Button(windraw_window, text="300", command="", width=20, height=3)
WB_250.place(x=550, y=80)
WB_250 = tk.Button(windraw_window, text="350", command="", width=20, height=3)
WB_250.place(x=550, y=130)
WB_250 = tk.Button(windraw_window, text="400", command="", width=20, height=3)
WB_250.place(x=550, y=180)
WB_250 = tk.Button(windraw_window, text="Cancle", command="", width=20, height=4)
WB_250.place(x=550, y=230)
Enter_WB = tk.Button(windraw_window, text="Enter", command="", width=20, height=3)
Enter_WB.place(x=0, y=230)
def pin_id():
e_c = int(e_EnterCard.get())
e_p = int(e_EnterPIN.get())
if e_c == 1:
if e_p == 1111:
new_window = tk.Tk()
new_window.resizable(False, False)
new_window.geometry("700x300")
window.destroy()
greet = tk.Label(
new_window,
text="Welcome to you Credit account!\n You have :"
+ str(CreditCardAmount),
padx=150,
pady=150,
)
greet.pack()
W_B = tk.Button(
new_window, text="windraw", command=Wintdrow_BB, width=30, height=4
)
W_B.pack()
W_B.place(x=0, y=30)
WH_B = tk.Button(
new_window, text="Windraw History", command=pin_id, width=30, height=4
)
WH_B.pack()
WH_B.place(x=500, y=30)
D_B = tk.Button(
new_window, text="Deposit", command=Deposit_BB, width=30, height=4
)
D_B.pack()
D_B.place(x=0, y=80)
DH_B = tk.Button(
new_window, text="Deposit History", command=pin_id, width=30, height=4
)
DH_B.pack()
DH_B.place(x=500, y=80)
T_B = tk.Button(
new_window, text="Transfer", command=pin_id, width=30, height=4
)
T_B.pack()
T_B.place(x=0, y=130)
TH_B = tk.Button(
new_window, text="Transfer History", command=pin_id, width=30, height=4
)
TH_B.pack()
TH_B.place(x=500, y=130)
C_B = tk.Button(
new_window, text="Cancle", command=pin_id, width=30, height=6
)
C_B.pack()
C_B.place(x=0, y=180)
B_B = tk.Button(new_window, text="Back", command="", width=30, height=6)
B_B.pack()
B_B.place(x=500, y=180)
else:
window.destroy()
elif e_c == 2:
if e_p == 2222:
new_credit_window = tk.Tk()
new_credit_window.geometry("700x300")
window.destroy()
greet = tk.Label(
new_credit_window,
text="Welcome to you Debit account!\n You have :"
+ str(DebitCardAmount),
padx=150,
pady=150,
)
greet.pack()
C_W_B = tk.Button(
new_credit_window,
text="windraw",
command="",
width=30,
height=4,
)
C_W_B.pack()
C_W_B.place(x=0, y=30)
C_WH_B = tk.Button(
new_credit_window,
text="Windraw History",
command=pin_id,
width=30,
height=4,
)
C_WH_B.pack()
C_WH_B.place(x=500, y=30)
C_D_B = tk.Button(
new_credit_window,
text="Deposit",
command="",
width=30,
height=4,
)
C_D_B.pack()
C_D_B.place(x=0, y=80)
C_DH_B = tk.Button(
new_credit_window,
text="Deposit History",
command=pin_id,
width=30,
height=4,
)
C_DH_B.pack()
C_DH_B.place(x=500, y=80)
C_T_B = tk.Button(
new_credit_window, text="Transfer", command=pin_id, width=30, height=4
)
C_T_B.pack()
C_T_B.place(x=0, y=130)
C_TH_B = tk.Button(
new_credit_window,
text="Transfer History",
command=pin_id,
width=30,
height=4,
)
C_TH_B.pack()
C_TH_B.place(x=500, y=130)
C_C_B = tk.Button(
new_credit_window, text="Cancle", command=pin_id, width=30, height=6
)
C_C_B.pack()
C_C_B.place(x=0, y=180)
C_B_B = tk.Button(
new_credit_window, text="Back", command="", width=30, height=6
)
C_B_B.pack()
C_B_B.place(x=500, y=180)
else:
window.destroy()
else:
window.destroy()
window = tk.Tk()
window.resizable(False, False)
window.geometry("300x300")
l_EnterCard = tk.Label(window, text=" Enter Card 1/2 : \n(1.Debit 2.Credit)")
l_EnterCard.place(x=40, y=120)
e_EnterCard = tk.Entry(window, width=10)
e_EnterCard.place(x=140, y=121)
l_PIN = tk.Label(window, text="PIN :")
l_PIN.place(x=40, y=180)
e_EnterPIN = tk.Entry(window, width=10, show="*")
e_EnterPIN.place(x=140, y=181)
B1 = tk.Button(window, text="Enter", command=pin_id, width=30)
B1.place(x=40, y=240)
window.mainloop()
```
Related
I'm having a hard time with calling the recordtable variable from my def btn3function() to my def update(). Anyone can help how I can call it without any error? Here's the full detail. I hope this will help, and thank you in advance again. I'm open to any suggestion or ideas. Willing to be enlightened.
reg = []
def register(name, number, date, time):
reg.append([name, number, date, time])
update()
def update(recordtable):
for item in recordtable.get_children():
recordtable.delete(item)
for r in range(len(reg)):
recordtable.insert(parent='', index='end', text='', iid=r, values=reg[r])
def btn3function(recordtable):
top = tkinter.Toplevel(root)
top.title("APPOINTMENTS")
frame = tk.Frame(top)
bg = ImageTk.PhotoImage(file="appointments.png")
labl = tk.Label(frame, image=bg)
labl.img = bg
labl.place(relx=0.5, rely=0.5, anchor='center')
frame.pack(pady=10)
frame.pack_propagate(False)
frame.configure(width=1000, height=790)
name = tk.Label(frame, text="Name:", font=('Bold', 16))
name.place(x=180, y=250)
nameEntry = tk.Entry(frame, font=('Bold', 16))
nameEntry.place(x=280, y=250, width=180)
number = tk.Label(frame, text="Number:", font=('Bold', 16))
number.place(x=180, y=300)
numberEntry = tk.Entry(frame, font=('Bold', 16))
numberEntry.place(x=280, y=300, width=180)
date = tk.Label(frame, text="Date:", font=('Bold', 16))
date.place(x=550, y=250)
dateEntry = tk.Entry(frame, font=('Bold', 16))
dateEntry.place(x=620, y=250, width=180)
time = tk.Label(frame, text="Time:", font=('Bold', 16))
time.place(x=550, y=300)
timeEntry = tk.Entry(frame, font=('Bold', 16))
timeEntry.place(x=620, y=300, width=180)
register = tk.Button(frame, text="Register", font=('Bold', 16))
register.place(x=270, y=380)
update = tk.Button(frame, text="Update", font=('Bold', 16))
update.place(x=400, y=380)
delete = tk.Button(frame, text="Delete", font=('Bold', 16))
delete.place(x=510, y=380)
clear = tk.Button(frame, text="Clear", font=('Bold', 16))
clear.place(x=620, y=380)
searchNum = tk.Label(frame, text="Search Appointment by Number:", font=('Bold', 14))
searchNum.place(x=210, y=450)
searchEntry = tk.Entry(frame, font=('Bold', 14))
searchEntry.place(x=500, y=450)
recordlabel = tk.Label(frame, text='Select Record to Update or Delete', bg='white', font=('Bold', 14))
recordlabel.pack(fill=tk.X)
recordlabel.place(x=350, y=500)
recordtable = ttk.Treeview(frame)
recordtable.pack(fill=tk.X, pady=5, side=BOTTOM)
recordtable['column'] = ['Name', 'Number', 'Date', 'Time']
recordtable.column('#0', anchor=tk.W, width=0, stretch=tk.NO)
recordtable.column('Name', anchor=tk.W, width=100)
recordtable.column('Number', anchor=tk.W, width=100)
recordtable.column('Date', anchor=tk.W, width=100)
recordtable.column('Time', anchor=tk.W, width=100)
recordtable.heading('Name', text="NAME", anchor=tk.W)
recordtable.heading('Number', text="NUMBER", anchor=tk.W)
recordtable.heading('Date', text="DATE", anchor=tk.W)
recordtable.heading('Time', text="TIME", anchor=tk.W)
Pass the recordtable variable into update as a parameter: def btn3function(rt). I'm not sure where you're actually calling update, but when you do you should do this: update(recordtable) somewhere after the declaration of recordtable
I am learning about the Tkinter and have created a Calculator using it. But now I want to write test cases so that I can verify things. But I don't know how to write test cases in Tkinter. I have tried to write the code but it seems like there is some error in it.
My Code:
calculator.py
from ast import operator
from distutils import text_file
from tkinter import *
from matplotlib import test
from numpy import number
def gui():
root = Tk()
root.title("Simple Calculator")
root.configure(bg="#1b4f72")
return root
textField = Entry(width=25, highlightthickness=5, bg="#d6eaf8", font="Cambria 11")
textField.configure(highlightbackground = "#1b4f72", highlightcolor= "#1b4f72")
textField.grid(row=0, column=0, columnspan=4)
root = gui()
def button_click(number):
value = textField.get()
textField.delete(0, END)
textField.insert(0, str(value)+str(number))
def buttonClear():
textField.delete(0, END)
def add():
global first
global operators
operators = "+"
first = textField.get()
textField.delete(0, END)
def subtract():
global first
global operators
operators = "-"
first = textField.get()
textField.delete(0, END)
def divide():
global first
global operators
operators = "/"
first = textField.get()
textField.delete(0, END)
def multiply():
global first
global operators
operators = "*"
first = textField.get()
textField.delete(0, END)
def equal():
second = textField.get()
textField.delete(0, END)
try:
if operators=="+":
textField.insert(0, int(first)+int(second))
elif operators=="-":
textField.insert(0, int(first)-int(second))
elif operators=="*":
textField.insert(0, int(first)*int(second))
elif operators=="/":
try:
textField.insert(0, int(first)/int(second))
except ZeroDivisionError:
textField.insert(0, "Zero Division")
except ValueError:
if operators=="+":
textField.insert(0, float(first)+float(second))
elif operators=="-":
textField.insert(0, float(first)-float(second))
elif operators=="*":
textField.insert(0, float(first)*float(second))
elif operators=="/":
try:
textField.insert(0, float(first)/float(second))
except ZeroDivisionError:
textField.insert(0, "Zero Division")
button_1 = Button(root, text="1", bg= '#2980b9', command=lambda: button_click(1), pady=10, padx=20, font="Cambria 11")
button_2 = Button(root, text="2", bg= '#2980b9', command=lambda: button_click(2), pady=10, padx=20, font="Cambria 11")
button_3 = Button(root, text="3", bg= '#2980b9', command=lambda: button_click(3), pady=10, padx=20, font="Cambria 11")
button_4 = Button(root, text="4", bg= '#2980b9', command=lambda: button_click(4), pady=10, padx=20, font="Cambria 11")
button_5 = Button(root, text="5", bg= '#2980b9', command=lambda: button_click(5), pady=10, padx=20, font="Cambria 11")
button_6 = Button(root, text="6", bg= '#2980b9', command=lambda: button_click(6), pady=10, padx=20, font="Cambria 11")
button_7 = Button(root, text="7", bg= '#2980b9', command=lambda: button_click(7), pady=10, padx=20, font="Cambria 11")
button_8 = Button(root, text="8", bg= '#2980b9', command=lambda: button_click(8), pady=10, padx=20, font="Cambria 11")
button_9 = Button(root, text="9", bg= '#2980b9', command=lambda: button_click(9), pady=10, padx=20, font="Cambria 11")
button_0 = Button(root, text="0", bg= '#2980b9', command=lambda: button_click(0), pady=10, padx=20, font="Cambria 11")
button_dot = Button(root, text=".", bg= '#2980b9', command=lambda: button_click("."), pady=10, padx=22, font="Cambria 11")
buttonClear = Button(root, text="Clear", bg= '#2980b9', command=buttonClear, pady=10, padx=97, font="Cambria 11")
buttonAdd = Button(root, text="+", bg= '#2980b9', command=add, pady=10, padx=20, font="Cambria 11")
buttonSubtract = Button(root, text="-", bg= '#2980b9', command=subtract, pady=10, padx=21, font="Cambria 11")
buttonMultiply = Button(root, text="X", bg= '#2980b9', command=multiply, pady=10, padx=20, font="Cambria 11")
buttonDivide = Button(root, text="/", bg= '#2980b9', command=divide, pady=10, padx=20, font="Cambria 11")
buttonEqual = Button(root, text="=", bg= '#2980b9', command=equal, pady=10, padx=20, font="Cambria 11")
button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)
button_0.grid(row=4, column=0)
button_dot.grid(row=4, column=1)
buttonEqual.grid(row=4, column=2)
buttonAdd.grid(row=1, column=3)
buttonSubtract.grid(row=2, column=3)
buttonMultiply.grid(row=3, column=3)
buttonDivide.grid(row=4,column=3)
buttonClear.grid(row=5, column=0, columnspan=4)
root.mainloop()
test_calculator.py
from calculator import *
def test_add():
button_click(4)
buttonAdd()
button_click(9)
buttonEqual()
assert textField.get() == 13
def test_subtract():
button_click(4)
buttonSubtract()
button_click(9)
buttonEqual()
assert textField.get() == -5
def test_multiply():
button_click(4)
buttonMultiply()
button_click(9)
buttonEqual()
assert textField.get() == 36
def test_divide():
button_click(9)
buttonDivide()
button_click(3)
buttonEqual()
assert textField.get() == 3
Any type of help will be appreciated.
Thanks
hello, i'm having an issue where, i can't make the size of buttons with letters be the same size as numbers as you can see if you run the program the button c is at different size of the others, i'm new here any tips on how to improve this would be gladly acepted,
my entire code is shown below.
at line 86 i set the button clear,and at 113, i set the grid of that button
from tkinter import *
import tkinter.font
root = Tk()
# color variables
number_colors = "black" # buttons
text_color = "white" # text inside entry
gray_layout = "#151414" # layout of the interface
gray_layout1 = "gray"
disp_backgroundcolors = "#3B3A3A"
# var that holds font displayed
font_disp = tkinter.font.Font(family="Segoe UI")
font_but = tkinter.font.Font(family="Segoe UI ", size=15)
# title of program
root.title("Calculator")
root.config(bg=gray_layout)
root.geometry("700x350")
e = Entry(root, width=0, fg="white", bg=gray_layout,
font=font_disp)
e.config(borderwidth=0)
e.grid(row=0, column=0, columnspan=3, sticky="NSEW")
Entry.rowconfigure(root, ALL, weight=1)
Entry.columnconfigure(root, ALL, weight=1)
Grid.rowconfigure(root, ALL, weight=1)
Grid.columnconfigure(root, ALL, weight=1)
def clicked_numbers(number):
current = e.get()
e.delete(0, END)
e.insert(0, str(current) + str(number))
def button_clear():
e.delete(0, END)
def button_add():
first_number = e.get()
global f_num
f_num = int(first_number)
e.delete(0, END)
def button_equal():
second_number = e.get()
e.delete(0, END)
e.insert(0, f_num + int(second_number))
# define buttons 1 , 9 to
ratiosize = 78
size_numberbuttons = 9
size_numberbuttonsy = 38
button_height = 1
button_width = 1
different_buttonsh = 3
different_buttonsw = 26
button_tickness = 0
button_1 = Button(root, text="1", padx=size_numberbuttonsy,
pady=size_numberbuttons, command=lambda: clicked_numbers(1), highlightthickness=button_tickness, highlightbackground="gray", borderwidth=button_tickness, bg=number_colors, fg=text_color, height=button_height, width=button_width)
button_2 = Button(root, text="2", padx=20,
pady=size_numberbuttons, command=lambda: clicked_numbers(2), bg=number_colors, fg=text_color, highlightthickness=button_tickness, borderwidth=button_tickness, height=button_height, width=button_width)
button_3 = Button(root, text="3", padx=size_numberbuttonsy,
pady=size_numberbuttons, command=lambda: clicked_numbers(3), bg=number_colors, fg=text_color, height=button_height, highlightthickness=button_tickness, borderwidth=button_tickness, width=button_width)
button_4 = Button(root, text="4", padx=size_numberbuttonsy,
pady=size_numberbuttons, command=lambda: clicked_numbers(4), bg=number_colors, fg=text_color, height=button_height, highlightthickness=button_tickness, borderwidth=button_tickness, width=button_width)
button_5 = Button(root, text="5", padx=size_numberbuttonsy,
pady=size_numberbuttons, command=lambda: clicked_numbers(5), bg=number_colors, fg=text_color, height=button_height, highlightthickness=button_tickness, borderwidth=button_tickness, width=button_width)
button_6 = Button(root, text="6", padx=size_numberbuttonsy,
pady=size_numberbuttons, command=lambda: clicked_numbers(6), bg=number_colors, fg=text_color, height=button_height, highlightthickness=button_tickness, borderwidth=button_tickness, width=button_width)
button_7 = Button(root, text="7", padx=size_numberbuttonsy,
pady=size_numberbuttons, command=lambda: clicked_numbers(7), bg=number_colors, fg=text_color, height=button_height, highlightthickness=button_tickness, borderwidth=button_tickness, width=button_width)
button_8 = Button(root, text="8", padx=size_numberbuttonsy,
pady=size_numberbuttons, command=lambda: clicked_numbers(8), bg=number_colors, fg=text_color, height=button_height, highlightthickness=button_tickness, borderwidth=button_tickness, width=button_width)
button_9 = Button(root, text="9", padx=size_numberbuttonsy,
pady=size_numberbuttons, command=lambda: clicked_numbers(9), bg=number_colors, fg=text_color, height=button_height, highlightthickness=button_tickness, borderwidth=button_tickness, width=button_width)
button_0 = Button(root, text="0", padx=size_numberbuttonsy,
pady=size_numberbuttons, command=lambda: clicked_numbers(0), bg=number_colors, fg=text_color, height=button_height, highlightthickness=button_tickness, borderwidth=button_tickness, width=button_width)
button_add1 = Button(root, text="+", padx=39,
pady=size_numberbuttons, command=button_add, bg=gray_layout1, fg="white", borderwidth=button_tickness, width=button_width, height=button_height)
button_equal1 = Button(root, text="=", height=different_buttonsh, width=different_buttonsw,
command=button_equal, bg=gray_layout1, fg="white", borderwidth=button_tickness)
button_clear1 = Button(root, text="C",
command=button_clear, bg=gray_layout1, fg="white", height=different_buttonsh, width=different_buttonsw, borderwidth=button_tickness)
# put buttons on screen
separatenum = 1
serparatenum1 = 1
button_1.grid(row=3, column=0, sticky="NSEW",
padx=separatenum, pady=serparatenum1)
button_2.grid(row=3, column=1, sticky="NSEW",
padx=separatenum, pady=serparatenum1)
button_3.grid(row=3, column=2, sticky="NSEW",
padx=separatenum, pady=serparatenum1)
button_4.grid(row=2, column=0, sticky="NSEW",
padx=separatenum, pady=serparatenum1)
button_5.grid(row=2, column=1, sticky="NSEW",
padx=separatenum, pady=serparatenum1)
button_6.grid(row=2, column=2, sticky="NSEW",
padx=separatenum, pady=serparatenum1)
button_7.grid(row=1, column=0, sticky="NSEW",
padx=separatenum, pady=serparatenum1)
button_8.grid(row=1, column=1, sticky="NSEW",
padx=separatenum, pady=serparatenum1)
button_9.grid(row=1, column=2, sticky="NSEW",
padx=separatenum, pady=serparatenum1)
button_0.grid(row=4, column=1, sticky="NSEW",
padx=separatenum, pady=serparatenum1)
button_clear1.grid(row=4, column=0, sticky="NSEW",
padx=separatenum, pady=serparatenum1)
button_add1.grid(row=4, column=2, sticky="NSEW",
padx=separatenum, pady=serparatenum1)
button_equal1.grid(row=4, column=3, columnspan=2, sticky="NSEW",
padx=separatenum, pady=serparatenum1+1)
button_1['font'] = font_but
button_2['font'] = font_but
button_3['font'] = font_but
button_4['font'] = font_but
button_5['font'] = font_but
button_6['font'] = font_but
button_7['font'] = font_but
button_8['font'] = font_but
button_9['font'] = font_but
button_0['font'] = font_but
root.mainloop()
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("게임이 종료되었습니다")
I am having a problem with, only having a single label to show in an instance of clicking the radio button. I want to show that label depending with the radio-button I clicked, I tried reading about pack/grid_forget, but I think I still don't understand it pretty well, or I am just doing it really wrong in my code.
from tkinter import *
root = Tk()
# I squeezed them inside a frame, just for aesthetic reasons.
btn_frame = LabelFrame(root, text='', padx=20, pady=20)
btn_frame.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
# I use button widgets, instead of other widgets.Because I felt buttons are square, and they are easy
to visualize.
red_btn = Button(btn_frame, text='R', padx=40, pady=40)
blue_btn = Button(btn_frame, text='B', padx=40, pady=40)
green_btn = Button(btn_frame, text='G', padx=40, pady=40)
red_btn.grid(row=0, column=0)
blue_btn.grid(row=0, column=1)
green_btn.grid(row=0, column=2)
def rad_btnClick(value):
red_Btn_press_label = Label(root, text='You press R!', padx=40, bg='red')
blu_Btn_press_label = Label(root, text='You press B!', padx=40, bg='blue')
grn_Btn_press_label = Label(root, text='You press G!', padx=40, bg='green')
if value == 1:
red_Btn_press_label.grid(row=2, column=0,)
red_btn = Button(btn_frame, text='r', padx=40, pady=40, bg='red')
red_btn.grid(row=0, column=0)
blu_Btn_press_label.grid_forget()
grn_Btn_press_label.grid_forget()
elif value == 2:
blu_Btn_press_label.grid(row=2, column=1)
blue_btn = Button(btn_frame, text='b', padx=40, pady=40, bg='blue')
blue_btn.grid(row=0, column=1)
red_Btn_press_label.grid_forget()
grn_Btn_press_label.grid_forget()
elif value == 3:
grn_Btn_press_label.grid(row=2, column=2)
green_btn = Button(btn_frame, text='g', padx=40, pady=40, bg='green')
green_btn.grid(row=0, column=2)
red_Btn_press_label.grid_forget()
blu_Btn_press_label.grid_forget()
radiopress = IntVar()
# LABEL, VALUE, COLUMN
RAD_BUTTONS = [('RED BTN', 1, 0),
('BLU BTN', 2, 1),
('GRN BTN', 3, 2)]
for color, value, column in RAD_BUTTONS:
rad_btn = Radiobutton(root, text=color, variable=radiopress, value=value, command= lambda:rad_btnClick(radiopress.get()))
rad_btn.grid(row=1, column=column)
root.mainloop()
So I am new in Python, I've just recently understood the 4/5 core fundamentals in every prog-language.
And now I am trying to learn about GUI with python, with Tkinter. I think my code got a bit longer, for something so simple and small. You can lecture my anyway you want, I just want learn good practices, and work on bad habits I may be doing.
I figured out that I should make my widgets global, and just make an update function to display every time an instance of them is created.
I want to thank Tim Roberts for helping me.
from tkinter import *
root = Tk()
btn_frame = LabelFrame(root, text='', padx=20, pady=20)
btn_frame.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
red_btn = Button(btn_frame, text='R', padx=40, pady=40)
blue_btn = Button(btn_frame, text='B', padx=40, pady=40)
green_btn = Button(btn_frame, text='G', padx=40, pady=40)
red_btn.grid(row=0, column=0)
blue_btn.grid(row=0, column=1)
green_btn.grid(row=0, column=2)
def rad_btnClick(value):
global rad_btn_press_label
global red_btn
global blue_btn
global green_btn
if value == 1:
rad_btn_press_label.grid_forget()
rad_btn_press_label = Label(root, text='You press R!', padx=40, bg='red')
red_btn = Button(btn_frame, text='R', padx=40, pady=40, bg='red')
blue_btn = Button(btn_frame, text='B', padx=40, pady=40)
green_btn = Button(btn_frame, text='G', padx=40, pady=40)
rad_btn_press_label.grid(row=2, column=0)
red_btn.grid(row=0, column=0)
blue_btn.grid(row=0, column=1)
green_btn.grid(row=0, column=2)
elif value == 2:
rad_btn_press_label.grid_forget()
rad_btn_press_label = Label(root, text='You press R!', padx=40, bg='blue')
red_btn = Button(btn_frame, text='R', padx=40, pady=40)
blue_btn = Button(btn_frame, text='B', padx=40, pady=40, bg='blue')
green_btn = Button(btn_frame, text='G', padx=40, pady=40)
rad_btn_press_label.grid(row=2, column=1)
red_btn.grid(row=0, column=0)
blue_btn.grid(row=0, column=1)
green_btn.grid(row=0, column=2)
elif value == 3:
rad_btn_press_label.grid_forget()
rad_btn_press_label = Label(root, text='You press G!', padx=40, bg='green')
red_btn = Button(btn_frame, text='R', padx=40, pady=40)
blue_btn = Button(btn_frame, text='B', padx=40, pady=40)
green_btn = Button(btn_frame, text='G', padx=40, pady=40, bg='green')
rad_btn_press_label.grid(row=2, column=2)
red_btn.grid(row=0, column=0)
blue_btn.grid(row=0, column=1)
green_btn.grid(row=0, column=2)
radiopress = IntVar()
# LABEL, VALUE, ROW, COLUMN
RAD_BUTTONS = [('RED BTN', 1, 1, 0),
('BLU BTN', 2, 1, 1),
('GRN BTN', 3, 1, 2)]
for color, value, row, column in RAD_BUTTONS:
rad_btn = Radiobutton(root, text=color, variable=radiopress, value=value, command=lambda: rad_btnClick(radiopress.get()))
rad_btn.grid(row=row, column=column)
rad_btn_press_label = Label(root, text='You press R!', padx=40,)
rad_btn_press_label.grid(row=2, column=0, )
rad_btn_press_label.grid_forget()
root.mainloop()