Calling a variable from a def function to another def function - python

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

Related

problem with set value in entrybox with button. Python Tkinter

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

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 update Labels in tkinter?

I'm trying to create a program in where you put a word in a box, press add, and this word goes to a list, which is also displayed on the right side. When I press the forward button the first thing on the list is deleted. Problem is I can't get the labels to update when I press the buttons / edit the list.
from tkinter import *
root = Tk()
root.title('Speakers List')
root.minsize(800, 600)
speakers = ['none']
spe = speakers[0]
def add():
if spe == 'none':
speakers.insert(0, [s])
e.delete(0, END)
spe.config(text=speakers[0])
else:
speakers[-2] = [s]
e.delete(0, END)
spe.config(text=speakers[0])
return
def forward():
if len(speakers) is 0:
return
else:
del speakers[0]
spe.config(text=speakers[0])
return
entry = StringVar()
e = Entry(root, width=30, font=("Arial", 20), textvariable=entry)
e.grid(row=0, sticky=W)
s = e.get()
button1 = Button(root, padx=10, pady=10, bd=5, text='Add', fg='black', command=add)
button1.grid(row=0, column=1)
button2 = Button(root, padx=10, pady=10, bd=5, text='Next', fg='black', command=forward)
button2.grid(row=1, column=1)
n = Label(root, font=("Arial", 35), bd=2, text=spe)
n.grid(row=1, sticky=W)
listdisplay = Label(root, font=('Arial', 20), text=speakers)
listdisplay.grid(row=0, column=10)
root.mainloop()
Is this the sort of thing you were looking for ?
from tkinter import *
root = Tk()
root.title('Speakers List')
root.minsize(800, 600)
speakers = ['50']
spe = speakers[0]
def add():
entry=e.get()
speakers.append(entry)
listdisplay.config(text=speakers)
return
def forward():
if len(speakers) is 0:
return
else:
del speakers[0]
listdisplay.config(text=speakers)
spe=speakers[0]
n.config(text=spe)
return
entry = StringVar()
e = Entry(root, width=30, font=("Arial", 20), textvariable=entry)
e.grid(row=0, sticky=W)
s = e.get()
button1 = Button(root, padx=10, pady=10, bd=5, text='Add', fg='black',command=add)
button1.grid(row=0, column=1)
button2 = Button(root, padx=10, pady=10, bd=5, text='Next', fg='black',command=forward)
button2.grid(row=1, column=1)
n = Label(root, font=("Arial", 35), bd=2, text=spe)
n.grid(row=1, sticky=W)
listdisplay = Label(root, font=('Arial', 20), text=speakers)
listdisplay.grid(row=0, column=10)
root.mainloop()
If so:
You create a list and then you use the append function to add an item to it. The rest was pretty much right.

Python tkinter, clearing an Entry widget from a class

This is the class I'm calling and the function from a different file
class CalcFunc:
def clearScreen(self):
self.log("CLEAR (CE)")
ent.delete(0, END)
This is the Entry Box
ent = Entry(root, textvariable=clc.getBtn, justify=RIGHT, font=10, relief=RIDGE, bd=2, width=15)
ent.grid(row=0, columnspan=3, pady=10)
This is the button I'm clicking to clear the Entry Box
buttonCC = Button(root, text="CLEAR (CE)", height=1, width=20, bg='orange', command=clc.clearScreen)
I'm not sure what the syntax is to be able to to clear an Entry widget from a class basically. That code worked when I had it in the same file but my project requires it to be in a separate file. It's a class project for a calculator and the "clear" button clears the Entry widget. I can post my entire code if that helps. Thank you.
----EDIT----
My Class
import time
class CalcFunc:
def log(self, val):
myFile = open(r".\log.dat", "a")
myFile.write("%s\n" % val)
myFile.close()
def onScreen(self, iVal):
self.log(iVal)
currentTxt = self.getBtn.get()
updateEnt = self.getBtn.set(currentTxt + iVal)
def clearScreen(self):
self.log("CLEAR (CE)")
ent.delete(0, END)
def evaL(self):
self.log("=")
self.getBtn.set(str(eval(self.getBtn.get())))
self.log(self.getBtn.get())
def logLbl(self):
myFile = open(r".\log.dat", "a")
myFile.write("\n==================================\n")
myFile.write("Date: " + str(time.strftime("%m/%d/%Y")) + " -- Time: " + str(time.strftime("%I:%M:%S")))
myFile.write("\n==================================\n")
myFile.close()
My Program
from tkinter import *
import time
import clcClass
root = Tk()
root.title('skClc v1')
clc = clcClass.CalcFunc()
clc.logLbl()
clc.getBtn = StringVar()
ent = Entry(root, textvariable=clc.getBtn, justify=RIGHT, font=10, relief=RIDGE, bd=2, width=15)
ent.grid(row=0, columnspan=3, pady=10)
button1 = Button(root, text="1", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('1'))
button2 = Button(root, text="2", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('2'))
button3 = Button(root, text="3", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('3'))
button4 = Button(root, text="4", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('4'))
button5 = Button(root, text="5", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('5'))
button6 = Button(root, text="6", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('6'))
button7 = Button(root, text="7", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('7'))
button8 = Button(root, text="8", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('8'))
button9 = Button(root, text="9", height=1, width=5, bg='light blue', command=lambda:clc.onScreen('9'))
button0 = Button(root, text="0", height=1, width=5, bg='light blue', command=lambda:onScreen('0'))
buttonP = Button(root, text="+", height=1, width=5, bg='gray', command=lambda:clc.onScreen('+'))
buttonM = Button(root, text="-", height=1, width=5, bg='gray', command=lambda:clc.onScreen('-'))
buttonMM = Button(root, text="x", height=1, width=5, bg='gray', command=lambda:clc.onScreen('*'))
buttonDD = Button(root, text="÷", height=1, width=5, bg='gray', command=lambda:clc.onScreen('/'))
buttonEE = Button(root, text="=", height=1, width=5, bg='light green', command=clc.evaL)
buttonCC = Button(root, text="CLEAR (CE)", height=1, width=20, bg='orange', command=clc.clearScreen)
button1.grid(row=1, column=0, pady=5)
button2.grid(row=1, column=1, pady=5)
button3.grid(row=1, column=2, pady=5)
button4.grid(row=2, column=0, pady=5)
button5.grid(row=2, column=1, pady=5)
button6.grid(row=2, column=2, pady=5)
button7.grid(row=3, column=0, pady=5)
button8.grid(row=3, column=1, pady=5)
button9.grid(row=3, column=2, pady=5)
button0.grid(row=4, column=0, pady=5)
buttonP.grid(row=4, column=1, pady=5)
buttonM.grid(row=4, column=2, pady=5)
buttonEE.grid(row=5, column=0, pady=5)
buttonDD.grid(row=5, column=1, pady=5)
buttonMM.grid(row=5, column=2, pady=5)
buttonCC.grid(row=6, column=0, pady=5, columnspan=3)
root.maxsize(140,245);
root.minsize(140,245);
root.mainloop()
ent = Entry(root, ....)
clc = clcClass.CalcFunc(ent)
class CalcFunc:
def __init__(self, entry):
self.entry = entry
def clearScreen(self):
self.log("CLEAR (CE)")
self.entry.delete(0, END)
Here's an abbreviated example:
#my_entry.py
from tkinter import END
import time
class EntryWithLogger:
def __init__(self, entry):
self.entry = entry
def log(self, val):
with open("log.dat", "a") as my_file: #Automatically closes the file--even if an exception occurs, which is not the case with my_file.close().
my_file.write("%s\n" % val)
def onScreen(self, i_val):
self.log(i_val)
self.entry.insert(END, i_val)
def clearScreen(self):
self.log("CLEAR (CE)")
self.entry.delete(0, END)
Note that I didn't use a StringVar(), which doesn't appear to be necessary. If you need it, you can always pass it as an argument to __init__(), then store it on self.
import my_entry as me
import tkinter as tk
root = tk.Tk()
root.title("Calculator")
root.geometry("+100+50") #("300x500+200+10") dimension, position
entry = tk.Entry(root, justify=tk.RIGHT, font=10, relief=tk.RIDGE, bd=2, width=15)
entry.grid(row=0, columnspan=3, pady=10)
entry_with_logger = me.EntryWithLogger(entry)
#Create the buttons in a loop:
for i in range(10):
row_num, col_num = divmod(i, 3) #divmod(7, 2) => (3, 1), divmod(0, 3) => (0, 0), divmod(4, 3) => (1, 1)
row_num += 1
button_text = str(i)
tk.Button(root, text=button_text,
height=1,
width=5,
bg='light blue',
command=lambda x=button_text: entry_with_logger.onScreen(x)
).grid(row=row_num, column=col_num, pady=5)
#Put the clear button at the bottom of the grid:
tk.Button(root, text="CLEAR (CE)",
height=1,
width=20,
bg='orange',
command=entry_with_logger.clearScreen
).grid(row=row_num+1, columnspan=3) #columnspan tells grid() to use 3 cells for the button,
#and the button will be centered by default.
root.mainloop()
Or, you could do it like this:
#my_entry.py
from tkinter import Entry, END
import time
class EntryWithLogger(Entry):
#Because __init__() is not implemented, the parent class's __init__() gets
#called, so you create an EntryWithLogger just like you would an Entry.
def log(self, val):
with open("log.dat", "a") as my_file: #Automatically closes the file--even if there is an exception, which is not the case with my_file.close().
my_file.write("%s\n" % val)
def onScreen(self, i_val):
self.log(i_val)
self.insert(END, i_val)
def clearScreen(self):
self.log("CLEAR (CE)")
self.delete(0, END)
import my_entry as me
import tkinter as tk
root = tk.Tk()
root.title("Calculator")
root.geometry("+100+50") #("300x500+200+10") dimension, position
entry = me.EntryWithLogger(root, justify=tk.RIGHT, font=10, relief=tk.RIDGE, bd=2, width=15)
entry.grid(row=0, columnspan=3, pady=10)
#Create the buttons in a loop:
for i in range(10):
row_num, col_num = divmod(i, 3) #divmod(7, 2) => (3, 1), divmod(0, 3) => (0, 0), divmod(4, 3) => (1, 1)
row_num += 1
button_text = str(i)
tk.Button(root, text=button_text,
height=1,
width=5,
bg='LightBlue',
command=lambda x=button_text: entry.onScreen(x)
).grid(row=row_num, column=col_num, pady=5)
#Put the clear button at the bottom of the grid:
tk.Button(root, text="CLEAR (CE)",
height=1,
width=20,
bg='orange',
command=entry.clearScreen
).grid(row=row_num+1, columnspan=3) #columnspan tells grid() to use 3 cells for the button,
#and the button will be centered by default.
root.mainloop()

Can convert Tkinter inputs into numbers

__author__ = 'Feuer'
from tkinter import *
root = Tk()
root.geometry("750x400")
def stop():
exit()
def plus():
global erg11
erg11 = z1 + z2
class Start:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.Label_1 = Label(frame, text="Bitte wählen Sie die Rechenart aus, die Sie benutzen möchten!")
self.Label_2 = Label(frame, text="Addition")
self.Label_3 = Label(frame, text="Subtraktion")
self.Label_4 = Label(frame, text="Multiplikation")
self.Label_5 = Label(frame, text="Division")
self.Label_6 = Label(frame, text="Wurzel")
self.Label_7 = Label(frame, text="Logarithmus")
self.Button_1 = Button(frame, text="Go!", command=Add)
self.Button_2 = Button(frame, text="Go!")
self.Button_3 = Button(frame, text="Go!")
self.Button_4 = Button(frame, text="Go!")
self.Button_5 = Button(frame, text="Go!")
self.Button_6 = Button(frame, text="Go!")
self.Button_7 = Button(frame, text="Das Programm beenden!", command=stop)
self.Label_1.grid(row=0, columnspan=2)
self.Label_2.grid(row=1, column=0)
self.Label_3.grid(row=2, column=0)
self.Label_4.grid(row=3, column=0)
self.Label_5.grid(row=4, column=0)
self.Label_6.grid(row=5, column=0)
self.Label_7.grid(row=6, column=0)
self.Button_1.grid(row=1, column=1)
self.Button_2.grid(row=2, column=1)
self.Button_3.grid(row=3, column=1)
self.Button_4.grid(row=4, column=1)
self.Button_5.grid(row=5, column=1)
self.Button_6.grid(row=6, column=1)
self.Button_7.grid(row=7, columnspan=2)
class Add:
def __init__(self):
newwin = Toplevel()
newwin.geometry("750x400")
frame2 = Frame(newwin)
frame2.pack()
global erg11
global z1
global z2
erg11 = "Ready"
self.Label_1 = Label(frame2, text="Additionsverfahren")
self.Entry_1 = Entry(frame2)
self.Label_2 = Label(frame2, text="+")
self.Entry_2 = Entry(frame2)
self.Label_3 = Label(frame2, text="=")
self.Button_1 = Button(frame2, text="Zurück", command=newwin.destroy)
self.Button_2 = Button(frame2, text="Ergebniss berechnen")
self.Label_Erg1 = Label(frame2, text=erg11)
self.Button_2.bind("<Button-1>", plus)
self.Label_1.grid(row=0, columnspan=4)
self.Entry_1.grid(row=1, column=0)
self.Label_2.grid(row=1, column=1)
self.Entry_2.grid(row=1, column=2)
self.Label_3.grid(row=1, column=3)
self.Button_2.grid(row=2, columnspan=4)
self.Button_1.grid(row=3, columnspan=4)
self.Label_Erg1.grid(row=1, column=4)
app = Start(root)
root.mainloop()
this is my code i am using at the moment.I try to create a little useless calculator with gui in Python. I cant figure out how to get the variable (z1 / z1) out of Entry_1 and _2 when someone is pressing the button_2 (in the second class). Could any one sugesst me some code to fix it?
Edit:
I edited the Code, that everybody could try to find a solution for my problem, because my solution approaches ended in a stalemate. (Ayres)
The content of your entries are read immediately after creation of them, leading get() to return an empty string that can't be converted.
The get method has to be called somewhere else, though I can't exactly say when.

Categories