ArfIsAToe

calculator that looks kind of good

Jun 8th, 2021 (edited)
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.70 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import Button, Menu, font
  3. from tkinter.constants import DISABLED, NORMAL
  4.  
  5.    
  6. def fitInScreen():
  7.     global onScreenText,myFont, fontSize, screenText, firstInt, secondInt,disabledBySize
  8.     #print("okay: "+str(len(onScreenText))+"\t"+str(fontSize))
  9.     if (len(onScreenText)>=32 and fontSize==20):
  10.         fontSize=15
  11.         myFont=font.Font(family="emojione color", size=fontSize)
  12.         screen["font"]=myFont
  13.        
  14.     if (len(onScreenText)>=49 and fontSize==15):
  15.         fontSize=20
  16.         myFont=font.Font(family="emojione color", size=fontSize)
  17.         screen["font"]=myFont
  18.         onScreenText="Error: integer overload"
  19.         screenText.set(onScreenText)        
  20.         plusButton["state"]=DISABLED
  21.         minusButton["state"]=DISABLED
  22.         divButton["state"]=DISABLED
  23.         timesButton["state"]=DISABLED
  24.         onScreenText=""
  25.         secondInt=0
  26.         firstInt=0
  27.         disabledBySize=True
  28.    
  29. def numtapped(x,button):
  30.     global screenText, onScreenText, lastIsNum, secondInt, justOperated, lastIsCalc, disabledBySize
  31.     if False==lastIsNum:
  32.         secondInt=secondInt*10+x
  33.         if justOperated:
  34.             onScreenText=onScreenText+globalOperator
  35.             plusButton["state"]=DISABLED
  36.             minusButton["state"]=DISABLED
  37.             divButton["state"]=DISABLED
  38.             timesButton["state"]=DISABLED
  39.             justOperated=False
  40.     elif lastIsCalc:
  41.         onScreenText=""
  42.         lastIsCalc=False
  43.    
  44.     if disabledBySize:
  45.         plusButton["state"]=NORMAL
  46.         minusButton["state"]=NORMAL
  47.         divButton["state"]=NORMAL
  48.         timesButton["state"]=NORMAL
  49.         disabledBySize=False
  50.  
  51.    
  52.     onScreenText=onScreenText+str(x)
  53.     fitInScreen()
  54.     screenText.set(onScreenText)
  55.  
  56. def operator(x):
  57.     global screenText, onScreenText, lastIsNum, firstInt, globalOperator, justOperated, secondInt
  58.     lastIsNum=False
  59.     firstInt=int(onScreenText)
  60.     screenText.set(onScreenText+x)
  61.     globalOperator=x
  62.     justOperated=True
  63.     secondInt=0
  64.     fitInScreen()
  65.  
  66. def equals():
  67.     global firstInt, secondInt, screenText, onScreenText, lastIsNum, globalOperator, lastIsCalc
  68.  
  69.     if (globalOperator==" + " ):
  70.         onScreenText=str(firstInt+secondInt)
  71.         fitInScreen()
  72.         screenText.set(onScreenText)
  73.         firstInt+=secondInt
  74.     elif (globalOperator== " - "):
  75.         onScreenText=str(firstInt-secondInt)
  76.         fitInScreen()
  77.         screenText.set(onScreenText)
  78.         firstInt-=secondInt
  79.     elif (globalOperator== " x "):
  80.         onScreenText=str(firstInt*secondInt)
  81.         fitInScreen()
  82.         screenText.set(onScreenText)
  83.         firstInt*=secondInt
  84.     elif (globalOperator== " / "):
  85.         onScreenText=str(int(firstInt/secondInt))
  86.         fitInScreen()
  87.         screenText.set(onScreenText)
  88.         firstInt/=secondInt
  89.    
  90.     plusButton["state"]=NORMAL
  91.     minusButton["state"]=NORMAL
  92.     divButton["state"]=NORMAL
  93.     timesButton["state"]=NORMAL
  94.  
  95.     lastIsNum=True
  96.     lastIsCalc=True
  97.  
  98. def changeTheme():
  99.     root.config(bg=backgroundColor)
  100.     screen["bg"]=bgColor
  101.     screen["fg"]=fgColor
  102.  
  103.     num0["bg"]=bgColor
  104.     num0["fg"]=fgColor
  105.     num0["activebackground"]=bgColorActive
  106.     num0["activeforeground"]=fgColor  
  107.  
  108.     num1["bg"]=bgColor
  109.     num1["fg"]=fgColor
  110.     num1["activebackground"]=bgColorActive
  111.     num1["activeforeground"]=fgColor
  112.  
  113.     num2["bg"]=bgColor
  114.     num2["fg"]=fgColor
  115.     num2["activebackground"]=bgColorActive
  116.     num2["activeforeground"]=fgColor
  117.  
  118.     num3["bg"]=bgColor
  119.     num3["fg"]=fgColor
  120.     num3["activebackground"]=bgColorActive
  121.     num3["activeforeground"]=fgColor
  122.  
  123.     num4["bg"]=bgColor
  124.     num4["fg"]=fgColor
  125.     num4["activebackground"]=bgColorActive
  126.     num4["activeforeground"]=fgColor
  127.  
  128.     num5["bg"]=bgColor
  129.     num5["fg"]=fgColor
  130.     num5["activebackground"]=bgColorActive
  131.     num5["activeforeground"]=fgColor  
  132.    
  133.     num6["bg"]=bgColor
  134.     num6["fg"]=fgColor
  135.     num6["activebackground"]=bgColorActive
  136.     num6["activeforeground"]=fgColor
  137.  
  138.     num7["bg"]=bgColor
  139.     num7["fg"]=fgColor
  140.     num7["activebackground"]=bgColorActive
  141.     num7["activeforeground"]=fgColor
  142.  
  143.     num8["bg"]=bgColor
  144.     num8["fg"]=fgColor
  145.     num8["activebackground"]=bgColorActive
  146.     num8["activeforeground"]=fgColor
  147.  
  148.     num9["bg"]=bgColor
  149.     num9["fg"]=fgColor
  150.     num9["activebackground"]=bgColorActive
  151.     num9["activeforeground"]=fgColor
  152.  
  153.  
  154.     plusButton["bg"]=bgColor
  155.     plusButton["fg"]=fgColor
  156.     plusButton["activebackground"]=bgColorActive
  157.     plusButton["activeforeground"]=fgColor
  158.  
  159.     minusButton["bg"]=bgColor
  160.     minusButton["fg"]=fgColor
  161.     minusButton["activebackground"]=bgColorActive
  162.     minusButton["activeforeground"]=fgColor
  163.  
  164.     equalButton["bg"]=bgColor
  165.     equalButton["fg"]=fgColor
  166.     equalButton["activebackground"]=bgColorActive
  167.     equalButton["activeforeground"]=fgColor
  168.  
  169.     divButton["bg"]=bgColor
  170.     divButton["fg"]=fgColor
  171.     divButton["activebackground"]=bgColorActive
  172.     divButton["activeforeground"]=fgColor
  173.  
  174.     timesButton["bg"]=bgColor
  175.     timesButton["fg"]=fgColor
  176.     timesButton["activebackground"]=bgColorActive
  177.     timesButton["activeforeground"]=fgColor
  178.  
  179. def Dark():      
  180.     global backgroundColor, bgColor, fgColor, bgColorActive, screen
  181.     backgroundColor="#272727"
  182.     bgColor="#353535"
  183.     bgColorActive="#515151"
  184.     fgColor="#BAF9FF"
  185.     changeTheme()
  186.  
  187. def Light():  
  188.     global backgroundColor, bgColor, fgColor, bgColorActive  
  189.     backgroundColor="#f5f5f5"
  190.     bgColor="#fff"
  191.     bgColorActive="#fafafa"
  192.     fgColor="#000"
  193.     changeTheme()
  194.  
  195. def clrscr():
  196.     global onScreenText, screenText, firstInt, secondInt
  197.     firstInt, secondInt= 0,0
  198.     onScreenText=""
  199.     screenText.set("Tap an equation")
  200.  
  201. lastIsCalc=True
  202. onScreenText=""
  203. lastIsNum=True
  204. firstInt, secondInt=0,0
  205. globalOperator=""
  206. justOperated=False
  207. disabledBySize=False
  208.  
  209. if True:#declaring colors    
  210.     backgroundColor="#f5f5f5"
  211.     bgColor="#fff"
  212.     bgColorActive="#fafafa"
  213.     fgColor="#000"
  214.  
  215. if True: #declaring Root, Font and Label
  216.     root= tk.Tk()
  217.     root.title("Calculator")
  218.     root.geometry("400x500+0+0")
  219.     root.config(bg=backgroundColor)
  220.     fontSize=20
  221.     myFont=font.Font(family="emojione color", size=fontSize)
  222.  
  223.     screenText=tk.StringVar()
  224.     screenText.set("Tap an equation")
  225.     screen= tk.Label(root,textvariable=screenText,bg=bgColor,fg=fgColor,font= myFont, justify='left', )#,width=300,height=400)
  226.     screen.place(relx=0,rely=0,relheight=.2,relwidth=1)
  227.  
  228. if True:#declaring all the buttons
  229.     num7 = Button(root, text="7", bg=bgColor, fg=fgColor,activebackground=bgColorActive, activeforeground=fgColor , bd=0, font=myFont, command= lambda : numtapped(7,num7) )
  230.     num7.place(relx=0,rely=.21, relwidth=.25, relheight=.2)
  231.  
  232.     num8 = Button(root, text="8", bg=bgColor, fg=fgColor,activebackground=bgColorActive, activeforeground=fgColor , bd=0, font=myFont, command= lambda : numtapped(8,num8) )
  233.     num8.place(relx=.26,rely=.21, relwidth=.25, relheight=.2)
  234.  
  235.     num9 = Button(root, text="9", bg=bgColor, fg=fgColor,activebackground=bgColorActive, activeforeground=fgColor , bd=0, font=myFont, command= lambda : numtapped(9,num9) )
  236.     num9.place(relx=.52,rely=.21, relwidth=.25, relheight=.2)
  237.  
  238.  
  239.     num4 = Button(root, text="4", bg=bgColor, fg=fgColor,activebackground=bgColorActive, activeforeground=fgColor , bd=0, font=myFont, command= lambda : numtapped(4,num4) )
  240.     num4.place(relx=0,rely=.42, relwidth=.25, relheight=.2)
  241.  
  242.     num5 = Button(root, text="5", bg=bgColor, fg=fgColor,activebackground=bgColorActive, activeforeground=fgColor , bd=0, font=myFont, command= lambda : numtapped(5,num5) )
  243.     num5.place(relx=.26,rely=.42, relwidth=.25, relheight=.2)
  244.  
  245.     num6 = Button(root, text="6", bg=bgColor, fg=fgColor,activebackground=bgColorActive, activeforeground=fgColor , bd=0, font=myFont, command= lambda : numtapped(6,num6) )
  246.     num6.place(relx=.52,rely=.42, relwidth=.25, relheight=.2)
  247.  
  248.  
  249.     num1 = Button(root, text="1", bg=bgColor, fg=fgColor,activebackground=bgColorActive, activeforeground=fgColor , bd=0, font=myFont, command= lambda : numtapped(1,num1) )
  250.     num1.place(relx=0,rely=.63, relwidth=.25, relheight=.2)
  251.  
  252.     num2 = Button(root, text="2", bg=bgColor, fg=fgColor,activebackground=bgColorActive, activeforeground=fgColor , bd=0, font=myFont, command= lambda : numtapped(2,num2) )
  253.     num2.place(relx=.26,rely=.63, relwidth=.25, relheight=.2)
  254.  
  255.     num3 = Button(root, text="3", bg=bgColor, fg=fgColor,activebackground=bgColorActive, activeforeground=fgColor , bd=0, font=myFont, command= lambda : numtapped(3,num3) )
  256.     num3.place(relx=.52,rely=.63, relwidth=.25, relheight=.2)
  257.  
  258.  
  259.     num0 = Button(root, text="0", bg=bgColor, fg=fgColor,activebackground=bgColorActive, activeforeground=fgColor , bd=0, font=myFont, command= lambda : numtapped(0,num0) )
  260.     num0.place(relx=.0,rely=.84, relwidth=.25, relheight=.18)
  261.  
  262.  
  263.  
  264.     minusButton = Button(root, text="-", bg=bgColor, fg=fgColor,activebackground=bgColorActive, activeforeground=fgColor , bd=0, font=myFont, command= lambda : operator(" - ") )
  265.     minusButton.place(relx=.52,rely=.84, relwidth=.25, relheight=.18)
  266.  
  267.     timesButton = Button(root, text="x", bg=bgColor, fg=fgColor,activebackground=bgColorActive, activeforeground=fgColor , bd=0, font=myFont, command= lambda : operator(" x ") )
  268.     timesButton.place(relx=.78,rely=.42, relwidth=.25, relheight=.2)
  269.  
  270.     plusButton = Button(root, text="+", bg=bgColor, fg=fgColor,activebackground=bgColorActive, activeforeground=fgColor , bd=0, font=myFont, command= lambda : operator(" + ") )
  271.     plusButton.place(relx=.26,rely=.84, relwidth=.25, relheight=.18)    
  272.  
  273.     equalButton = Button(root, text="=", bg=bgColor, fg=fgColor,activebackground=bgColorActive, activeforeground=fgColor , bd=0, font=myFont, command= lambda : equals() )
  274.     equalButton.place(relx=.78,rely=.63, relwidth=.25, relheight=.41)
  275.  
  276.     divButton = Button(root, text="/", bg=bgColor, fg=fgColor,activebackground=bgColorActive, activeforeground=fgColor , bd=0, font=myFont, command= lambda : operator(" / ") )
  277.     divButton.place(relx=.78,rely=.21, relwidth=.25, relheight=.2)
  278.  
  279. if True:#declaring Menu
  280.     main = Menu(root, )
  281.     root.config(menu=main, )
  282.     App  = Menu(main)
  283.     Theme= Menu(main)
  284.  
  285.     App.add_command(label="Clear Screen", command= clrscr)
  286.     App.add_command(label="Exit Application", command= lambda : root.destroy())
  287.     main.add_cascade(label="App", menu=App,)
  288.  
  289.     Theme.add_command(label="Dark Theme" , command= Dark )
  290.     Theme.add_command(label="Light Theme", command= Light)
  291.     main.add_cascade(label="Themes", menu=Theme)
  292.  
  293. root.mainloop()
  294. """
  295. dark theme
  296. foreground #353535
  297. background #272727
  298. text       #BAF9FF
  299. """
Add Comment
Please, Sign In to add comment