PICTURE
Hi, I am making a specific calculator(which, does only the same function)
I am trying to make it as similar to the picture shown as possible
This is what I have done so far:
from tkinter import *
root = Tk()
root.geometry('250x150')
firstLabel = Label(root, text='Enter the property value: $').grid(row=1, column=3)
Input = Entry(root).grid(row=1, column=4, padx=5, pady=5)
secondLabel = Label(root, text='Assessment Value:').grid(row=2, column=3)
thirdLabel = Label(root,text='Property Tax:').grid(row=3, column=3)
Button1 = Button(root, text='Calculate').grid(row=4, padx=5, pady=5, column=4)
root.mainloop()
I need you assistance in how to complete it
You have to add command = lambda:function() in your button.
I created a Reverse Polish calculator. The logic will be same you can use this code for reference.
from tkinter import *
import math
import operator
class myapp(Frame):
def __init__ (self,parent):
Frame.__init__(self ,parent)
self.parent=parent
self.initUI()
def initUI(self):
self.parent.title("REVERSE POLISH CALCULATOR")
self.pack( fill=BOTH,expand=True)
frame1 = Frame(self)
frame1.place(x=20,y=30)
frame2 = Frame(self)
frame2.place(x=550,y=45)
frame3 = Frame(self)
frame3.place(x=20,y=100)
frame4 = Frame(self)
frame4.place(x=155,y=113)
frame5 = Frame(self)
frame5.place(x=255,y=213)
operator_list = {'+' : operator.add,
'-' : operator.sub,
'/' : operator.truediv,
'*' : operator.mul,
'^' : operator.pow,
}
def rpn():
cop=0
cnum=0
global res
stack = []
expression=w.get("1.0","end-1c")
labele1=Label(frame5,text="Enter valid Expression")
for i in expression.split(' '):
if i in operator_list:
cop+=1
op1=float(stack.pop())
op2=float(stack.pop())
result=operator_list[i](op1,op2)
stack.append(result)
else:
cnum+=1
stack.append((i))
res=stack.pop()
w2.configure(state="normal")
w2.insert(END,res)
w2.configure(state="disabled")
is_valid=[True if len(expression.split(' '))>2 and cnum==cop+1 else False]
print(is_valid)
if not is_valid:
labele1.pack()
labele1.config(text='yoooo',width=20)
if is_valid :
labele1.pack()
labele1.config(text='wrrroo',width=20)
label = Label( frame1 , text = "EXPRESSION : ",width = 14 , font = "Verdana 10 bold")
label.pack( side = LEFT ,padx = 0 , pady = 15 )
w = Text(frame1,width=35,height=1 ,font=("Helvetica",15))
#w.tag_configure('bold_italics', font=('Arial', 12, 'bold', 'italic'))
w.pack(side="left",fill=X)
#w.bind("<Return>", lambda event: callback(event,frame1))
w.pack(side = "left" , fill = X ,padx = 5 , pady = 15 )
button=Button(frame2 , width = 10 , text = "Calculate" ,font=("Arial",10,"bold"), command = lambda:rpn())
button.pack()
label = Label( frame3 , text = "RESULT : ",width = 10 , font = "Verdana 10 bold")
label.pack( side = LEFT ,padx = 0 , pady = 15 )
w2 = Text(frame4,width=35,height=1 ,font=("Helvetica",15))
w2.configure(state="disabled")
#w.tag_configure('bold_italics', font=('Arial', 12, 'bold', 'italic'))
w2.pack(side="left",fill=X)
#result = rpn(expression)
#print(result)
def main():
root = Tk()
root.geometry("700x500")
#root.resizable(width=False, height=False)
app = myapp(root)
root.mainloop()
if __name__ == '__main__':
main()
Related
i am new to programming and i am programming my first project and i want to know can i do a calculation in two text boxes and get the result to another one
And thank you guys
Here is my code:
from Tkinter import *
import Tkinter as tk
def add(x, y):
return x+y
root = tk.Tk()
root.geometry('340x275+50+50')
root.title('Calculator')
root.resizable(True, True)
root.iconbitmap("C://123//
icon.ico")
num_1 = Label(root,
text="First number :")
num_1.grid(row=1, column=1)
num_2 = Label(root,
text="Second number :")
num_2.grid(row=1, column=3)
textBox1 = Text(root,
height=2, width=10)
textBox1.grid(row=1, column=2)
input1 = textBox1.get("1.0",
END)
textBox2 = Text(root,
height=2, width=10)
textBox2.grid(row=1, column=4)
input2 = textBox2.get("1.0",
END)
textBox_result = Text(root,
height=2, width=40)
textBox_result.grid(row=3,
column=2)
btn_add = Button(root,
text="+", command=add(input1,
input2))
btn_add.grid(row=2, column=1)
root.mainloop()
As newbie tutorial. Try this;
from tkinter import *
root = Tk()
root.title("Sum Two Numbers With GUI Using Python Source Code")
width = 400
height = 280
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
root.geometry("%dx%d+%d+%d" % (width, height, x, y))
root.resizable(0, 0)
#==============================METHODS========================================
def calculate():
if not num1.get() and not num2.get():
print("Please enter a number")
else:
res=int(num1.get())+int(num2.get())
lbl_text.config(text = "The answer is %d" % (res))
num1.set=""
num2.set=""
#==============================FRAMES=========================================
Top = Frame(root, bd=2, relief=RIDGE)
Top.pack(side=TOP, fill=X)
Form = Frame(root, height=200)
Form.pack(side=TOP, pady=20)
#==============================LABELS=========================================
lbl_title = Label(Top, text = "Sum Two Numbers With GUI Using Python Source Code", font=('arial', 12))
lbl_title.pack(fill=X)
lbl_num1 = Label(Form, text = "Number 1:", font=('arial', 14), bd=15)
lbl_num1.grid(row=0, sticky="e")
lbl_num2 = Label(Form, text = "Number 2:", font=('arial', 14), bd=15)
lbl_num2.grid(row=1, sticky="e")
lbl_text = Label(Form)
lbl_text.grid(row=2, columnspan=2)
#==============================ENTRY WIDGETS==================================
num1 = Entry(Form, font=(14))
num1.grid(row=0, column=1)
num2 = Entry(Form, font=(14))
num2.grid(row=1, column=1)
#==============================BUTTON WIDGETS=================================
btn_calculate = Button(Form, text="Calculate", width=45, command=calculate)
btn_calculate.grid(pady=25, row=3, columnspan=2)
Another one easy for you.
import tkinter as tk
from functools import partial
def findsum(l3, num1, num2):
n1 = int(num1.get())
n2 = int(num2.get())
n3 = n1 + n2
l3.config(text="Result = %d" % n3)
return
root = tk.Tk()
root.title("Xiith.com")
root.geometry("700x500")
number1 = tk.StringVar()
number2 = tk.StringVar()
l1 = tk.Label(root, text="Enter 1st number").place(x=20, y=60)
l2 = tk.Label(root, text="Enter 2nd number").place(x=20, y=120)
t1 = tk.Entry(root, textvariable=number1).place(x=200, y=60)
t2 = tk.Entry(root, textvariable=number2).place(x=200, y=120)
labelResult = tk.Label(root)
labelResult.place(x=250, y=200)
findsum = partial(findsum, labelResult, number1, number2)
b1 = tk.Button(root, text="ADD", command=findsum).place(x=200, y=300)
b2 = tk.Button(root, text="Cancel", command=root.destroy).place(x=250, y=300)
root.mainloop()
When the "view" button is pressed, it should trigger the function solution(i) such that label should be displayed in the new window. The problem is that the window opens and the previous label is packed but the label which gets it's text from "i" does not gets packed, Is there any issue in passing the parameter.
Any help is appreciated.
root = Tk()
root.config(background = "#303939")
root.state('zoomed')
def pre():
with open("DoubtSolution.txt","r+") as f:
dousol = f.read()
dousol_lst = dousol.split("`")
k = 0
window = Tk()
window.config(background = "#303939")
window.state('zoomed')
predoubt = Label(window,
text="Previous Doubts",
fg="Cyan",
bg="#303939",
font="Helvetica 50 bold"
).grid(row=0, column=1)
def solution(text):
print(text)
window1 = Tk()
window1.config(background="#303939")
window1.state('zoomed')
sol = Label(window1,
text=text[:text.find("~")],
font=font.Font(size=20),
bg="#303939",
fg="Cyan")
sol.pack()
window1.mainloop()
for i in dousol_lst:
if i[-5:] == admno:
doubt = Label(window, text=i[i.find("]]")+2:i.find("}}}}")], font=font.Font(size=20), bg="#303939",
fg="Cyan")
doubt.grid(row=2+k, column=1, pady=10)
view = Button(
master=window,
text="View", font=font.Font(size=15, family="Helvetica"),
activebackground="White",
bg="Teal",
bd=0.8,
fg="White",
command = lambda k = k:solution(i)
)
view.grid(row=2+k, column=2, padx=20)
k=k+1
window.mainloop()
previous = Button(
master=root,
text="Previous Doubts", font="Helvetica 22 bold",
activebackground="White",
bg="Teal",
bd=0.8,
fg="White",
command = pre
).grid(row=4, column=3, padx=20)
root.mainloop()
Okay so I have this sign up form and there is a part where you have to enter your name, I want that name answer to be taken to the page after.
import tkinter as tk
root = tk.Tk()
root.geometry("150x50+680+350")
def FormSubmission():
global button_start
button_start.place_forget()
l1.place_forget()
root.attributes("-fullscreen", True)
frame = tk.Frame(root)
tk.Label(frame, text="First Name:").grid(row=0)
name = entry1 = tk.Entry(frame) # I want the name written here to be taken from here to the welcome text.
entry1.grid(row=0, column=1)
tk.Label(frame, text="Last Name:").grid(row=1)
e2 = tk.Entry(frame)
e2.grid(row=1, column=1)
tk.Label(frame, text="Email:").grid(row=2)
e3 = tk.Entry(frame)
e3.grid(row=2, column=1)
tk.Label(frame, text="Date of Birth:").grid(row=3)
e4 = tk.Entry(frame)
e4.grid(row=3, column=1)
frame.pack(anchor='center', expand=True)
button_next = tk.Button(frame, text = "Next", height = 2, width = 7, command =lambda: MainPage(frame))
button_next.grid(row=4, column=1)
def MainPage(frame):
global FormSubmission
frame.pack_forget()
root.attributes("-fullscreen", True)
l1.place(x = 500, y = 10)
button_start.place_forget()
l1 = tk.Label(root, text="Welcome," , font=("Arial", 44)) #As you can see here in this line I want the entry 1 name here after welcome and the comma
button_start = tk.Button(root, text="Start", height=3, width=20, command = FormSubmission)
button_start.place(x = 0, y = 10)
button_exit = tk.Button(root, text="Exit", command=root.destroy)
button_exit.place(x=1506, y=0)
root.mainloop()
What I want to do is take the entry 1 answer and put it in the welcome text. There is an indicator on the lines I'm talking about.
Here is an example how to provide text from your widget entry1
in function FormSubmission():where you are defining your button you should pass the text you want to show in your label
button_next = tk.Button(frame, text = "Next", height = 2, width = 7, command =lambda: MainPage(frame, entry1.get()))
in funtion MainPage(frame):you should set text to your label:
def MainPage(frame, entry1):
global FormSubmission
frame.pack_forget()
root.attributes("-fullscreen", True)
l1.place(x = 500, y = 10)
button_start.place_forget()
l1.config(text="Welcome," + entry1) #<-------
I want to link my radiobuttons with the title, like merge my title with the radio button. This is the code which I implemented:
but1 = Radiobutton(text = "Current",value = 0)
but1.place(x =400,y = 150)
but2 = Radiobutton(text = "Previous",value = 1)
but2.place(x =320,y = 150)
but3 = Radiobutton(text = "Current",value = 2)
but3.place(x =400,y = 260)
but4 = Radiobutton(text = "Previous",value = 3)
but4.place(x =320,y = 260)
the_window.geometry("510x430")
label1 = Label(the_window,text = "Most-Discussed \n TV SHOW", font =
"Times 10 bold")
label1.place(x = 350,y = 110)
label2 = Label(the_window,text = "Most-Discussed \n TV SHOW", font =
"Times 10 bold")
label2.place(x = 350,y = 230)
This is the actual result:
This is the expected result:
I don't know if you know this, but that widget is called a LabelFrame. See the below example.
P.S. I have changed your geometry manager to grid
import tkinter as tk
root = tk.Tk()
f1 = tk.LabelFrame(root, text="Most-Discussed \n TV SHOW", labelanchor="n", font="Verdana 12 bold", bd=4)
f1.grid(row=0, column=0, padx=10, pady=10)
f2 = tk.LabelFrame(root, text="Music Vinyl \n Album Chart", labelanchor="n", font="Verdana 12 bold", bd=4)
f2.grid(row=1, column=0, padx=10, pady=10)
but1 = tk.Radiobutton(f1, text="Current", value = 0)
but1.grid(row=0, column=0)
but2 = tk.Radiobutton(f1, text="Previous" ,value = 1)
but2.grid(row=0, column=1)
but3 = tk.Radiobutton(f2, text="Current", value = 2)
but3.grid(row=0, column=0)
but4 = tk.Radiobutton(f2, text="Previous" ,value = 3)
but4.grid(row=0, column=1)
root.mainloop()
I have a homework assignment that I almost have completed, but the scrollbar does not function as expected. While it works when I'm focused on the Text, it does not work when I try to move the scrollbar or use the arrows.
from tkinter import *
class LoanCalculator:
def __init__(self):
window = Tk()
window.title("Loan Calculator")
frame = Frame(window)
frame.pack()
Label(frame, text="Loan Amount").grid(row=1,column=1, sticky=W)
self.loanAmmount = StringVar()
self.entryLoan = Entry(frame, textvariable=self.loanAmmount, justify=RIGHT)
Label(frame, text="Years").grid(row=1, column=3,sticky=W)
self.years = StringVar()
self.entryYears = Entry(frame, textvariable=self.years, justify=RIGHT)
btCalc = Button(frame, text="Calculate Loan", command=self.Calculate)
scrollbar = Scrollbar(frame)
self.text = Text(frame, width=60, height=10,wrap=WORD, yscrollcommand=scrollbar.set)
self.entryLoan.grid(row=1, column=2)
self.entryYears.grid(row=1, column=4)
btCalc.grid(row=1, column=5)
self.text.grid(row=2,column=1,columnspan=4)
scrollbar.grid(row=2, column=5, sticky="NSW")
window.mainloop()
def Calculate(self):
self.text.delete("1.0", END)
self.text.insert(END, "{0:<20s}{1:<20s}{2:<20s}".format("Interest Rate", "Monthly Payment", "Total Payment"))
aIR = 5.0
mIR = 0
mP = 0
tP = 0
fLA = 0
fYear = 0
lA = 0
year = 0
textToOut = ""
while aIR <= 8.0:
fLA = self.loanAmmount.get()
fYear = self.years.get()
lA = int(fLA)
year = int(fYear)
mIR = aIR / 1200
mP = lA * mIR / (1 - (pow(1 / (1 + mIR), year * 12)))
tP = mP * year * 12
textToOut = format(aIR, ">5.3f") + "%" + format(mP, "20.2f") + format(tP, "20.2f") + "\n"
self.text.insert(END, textToOut)
aIR += 1.0 / 8
LoanCalculator()
EDIT
I've changed my question to drop the text portion, as I have figured a workaround for that.
You need to configure the scrollbar to run an action (update the view of the text area) on scrolling. Add this line of code after you defined your text area.
scrollbar.config(command=self.text.yview)
So the code block of __init__ would be something like this:
def __init__(self):
window = Tk()
window.title("Loan Calculator")
frame = Frame(window)
frame.pack()
Label(frame, text="Loan Amount").grid(row=1,column=1, sticky=W)
self.loanAmmount = StringVar()
self.entryLoan = Entry(frame, textvariable=self.loanAmmount, justify=RIGHT)
Label(frame, text="Years").grid(row=1, column=3,sticky=W)
self.years = StringVar()
self.entryYears = Entry(frame, textvariable=self.years, justify=RIGHT)
btCalc = Button(frame, text="Calculate Loan", command=self.Calculate)
scrollbar = Scrollbar(frame)
self.text = Text(frame, width=60, height=10,wrap=WORD, yscrollcommand=scrollbar.set)
scrollbar.config(command=self.text.yview)
self.entryLoan.grid(row=1, column=2)
self.entryYears.grid(row=1, column=4)
btCalc.grid(row=1, column=5)
self.text.grid(row=2,column=1,columnspan=4)
scrollbar.grid(row=2, column=5, sticky="NSW")