Advertisement
furas

Python - tkinter - global - grid

May 21st, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.65 KB | None | 0 0
  1.  
  2.  
  3. from tkinter import *
  4. from tkinter import ttk
  5. from tkinter import messagebox
  6. import math
  7.  
  8. #Funciones
  9.  
  10. def calcularSuma():
  11.     global resultado
  12.    
  13.     numero1 = num1.get() # Se convierten las variables de Tkinter a variables de Python
  14.     numero2 = num2.get()
  15.  
  16.     resultado = numero1 + numero2
  17.  
  18.     Label(pestañaSuma, text = "Total: %s" %(resultado)).grid(row=2, column=1)
  19.    
  20.  
  21.  
  22. def calcularResta():
  23.     global resultado
  24.  
  25.     numero1 = num1.get()
  26.     numero2 = num2.get()
  27.  
  28.     resultado = numero1 - numero2
  29.  
  30.     Label(pestañaResta, text = "Total: %s" %(resultado)).place(x = 80, y = 200)
  31.  
  32. def calcularMultipl():
  33.     global resultado
  34.  
  35.     numero1 = num1.get()
  36.     numero2 = num2.get()
  37.  
  38.     resultado = numero1 * numero2
  39.  
  40.     Label(pestañaMult, text = "Total: %s" %(resultado)).place(x = 80, y = 200)
  41.  
  42. def calcularDivn():
  43.     global resultado
  44.  
  45.     numero1 = num1.get()
  46.     numero2 = num2.get()
  47.  
  48.     if numero2 == 0: # Evita que se pueda dividir por cero. La rasón por la cual uso un if, envez de un try es debido a que en Tkinter no me funcionaron :P
  49.         # Label(pestañaDivn, text = "No se puede dividir por cero", fg = "#DC143C").place(x = 80, y = 200)
  50.         messagebox.showwarning("Error!", "No se puede dividir por cero.")
  51.     else:
  52.         resultado = numero1 / numero2
  53.  
  54.         Label(pestañaDivn, text = "Total: %s" %(resultado)).place(x = 80, y = 200)
  55.        
  56. def calcularRaizC():
  57.     global resultado
  58.  
  59.     numero1 = num1.get()
  60.  
  61.     if numero1 >= 0:
  62.         resultado = math.sqrt(numero1)
  63.         Label(pestañaRaizC, text = "Total: %s" %(resultado)).place(x = 80, y = 200)
  64.     else:
  65.         messagebox.showwarning("Error!", "Solo se puede obtener valores arriba del 0")
  66.         # Label(pestañaRaizC, text = "Solo se puede obtener valores arriba del 0", fg = "#DC143C").place(x = 80, y = 200)
  67.  
  68. def calcularElevacn():
  69.     global resultado
  70.  
  71.     numero1 = num1.get()
  72.     numero2 = num2.get()
  73.  
  74.     resultado = math.pow(numero1, numero2)
  75.  
  76.     Label(pestañaElevacn, text = "Total: %s" %(resultado)).place(x = 80, y = 200)
  77.  
  78. def calcularCos():
  79.     global resultado
  80.  
  81.     numero1 = num1.get()
  82.  
  83.     resultado = math.cos(numero1)
  84.  
  85.     Label(pestañaCos, text = "Total: %s" %(resultado)).place(x = 80, y = 200)
  86.  
  87. def calcularSin():
  88.     global resultado
  89.     numero1 = num1.get()
  90.  
  91.     resultado = math.sin(numero1)
  92.  
  93.     Label(pestañaSin, text = "Total: %s" %(resultado)).place(x = 80, y = 200)
  94.  
  95. def calcularTan():
  96.     global resultado
  97.     numero1 = num1.get()
  98.  
  99.     resultado = math.tan(numero1)
  100.  
  101.     Label(pestañaTan, text = "Total: %s" %(resultado)).place(x = 80, y = 200)
  102.  
  103. def calcularArctan():
  104.     global resultado
  105.     numero1 = num1.get()
  106.      
  107.     resultado = math.atan(numero1)
  108.      
  109.     Label(pestañaArctan, text = "Total: %s" % (resultado)).place(x = 80, y = 200)
  110.      
  111. def calcularArCos():
  112.     global resultado
  113.     numero1 = num1.get()
  114.      
  115.     resultado = math.acos(numero1)
  116.      
  117.     Label(pestañaArCos, text = "Total: %s" % (resultado)).place(x = 80, y = 200)
  118.      
  119. def calcularArSin():
  120.     global resultado
  121.     numero1 = num1.get()
  122.      
  123.     resultado = math.asin(numero1)
  124.      
  125.     Label(pestañaArSin, text = "Total: %s" % (resultado)).place(x = 80, y = 200)
  126.      
  127. def calcularSinh():
  128.     global resultado
  129.     numero1 = num1.get()
  130.      
  131.     resultado = math.sinh(numero1)
  132.      
  133.     Label(pestañaSinh, text = "Total: %s" % (resultado)).place(x = 80, y = 200)
  134.      
  135.        
  136. def calcularCosh():
  137.     global resultado
  138.     numero1 = num1.get()
  139.      
  140.     resultado = math.cosh(numero1)
  141.      
  142.     Label(pestañaCosh, text = "Total: %s" % (resultado)).place(x = 80, y = 200)
  143.      
  144. def calcularTanh():
  145.     global resultado
  146.     numero1 = num1.get()
  147.      
  148.     resultado = math.tanh(numero1)
  149.      
  150.     Label(pestañaTanh, text = "Total: %s" % (resultado)).place(x = 80, y = 200)
  151.      
  152.          
  153. def calcularArCosh():
  154.     global resultado
  155.     numero1 = num1.get()
  156.      
  157.     resultado = math.acosh(numero1)
  158.      
  159.     Label(pestañaArCosh, text = "Total: %s" % (resultado)).place(x = 80, y = 200)
  160.      
  161.          
  162. def calcularArSinh():
  163.     global resultado
  164.     numero1 = num1.get()
  165.      
  166.     resultado = math.asinh(numero1)
  167.      
  168.     Label(pestañaArSinh, text = "Total: %s" % (resultado)).place(x = 80, y = 200)
  169.      
  170.          
  171. def calcularArTanh():
  172.     global resultado
  173.     numero1 = num1.get()
  174.      
  175.     resultado = math.atanh(numero1)
  176.      
  177.     Label(pestañaArTanh, text = "Total: %s" % (resultado)).place(x = 80, y = 200)
  178.      
  179. def calcularLog():
  180.     global resultado
  181.     numero1 = num1.get()
  182.  
  183.     resultado = math.log(numero1)
  184.  
  185.     Label(pestañaLog, text = "Total: %s" %(resultado)).place(x = 80, y = 200)
  186.  
  187.  
  188. def put_in_num1():
  189.     num1.set(resultado)
  190.  
  191. def put_in_num2():
  192.     num2.set(resultado)
  193.    
  194. #Codigo Principal
  195.  
  196. ventanaPrincipal = Tk()
  197.  
  198. resultado = 0 # assign value at start
  199.  
  200. num1 = DoubleVar() # Se declara variable para los numeros
  201. num2 = DoubleVar()
  202.  
  203. ventanaPrincipal.geometry("800x600")
  204. ventanaPrincipal.title("Calculadora Cientifica")
  205. #ventanaPrincipal.iconbitmap('icon.ico') # Le pone un icono a la ventana
  206.  
  207. notebook = ttk.Notebook(ventanaPrincipal) # Crea las pestañas
  208. notebook.pack(fill = 'both', expand = 'yes') # La pestaña se expande si no colisiona con ningun objeto
  209.  
  210. pestañaSuma = ttk.Frame(notebook) # Crea la ventana
  211. pestañaResta = ttk.Frame(notebook)
  212. pestañaMult = ttk.Frame(notebook)
  213. pestañaDivn = ttk.Frame(notebook)
  214. pestañaRaizC = ttk.Frame(notebook)
  215. pestañaElevacn = ttk.Frame(notebook)
  216. pestañaCos = ttk.Frame(notebook)
  217. pestañaSin = ttk.Frame(notebook)
  218. pestañaTan = ttk.Frame(notebook)
  219. pestañaLog = ttk.Frame(notebook)
  220. pestañaArctan = ttk.Frame(notebook)
  221. pestañaSinh = ttk.Frame(notebook)
  222. pestañaCosh = ttk.Frame(notebook)
  223. pestañaTanh = ttk.Frame(notebook)
  224. pestañaArCosh = ttk.Frame(notebook)
  225. pestañaArSinh = ttk.Frame(notebook)
  226. pestañaArTanh = ttk.Frame(notebook)
  227. pestañaArCos = ttk.Frame(notebook)
  228. pestañaArSin = ttk.Frame(notebook)
  229.  
  230.  
  231. notebook.add(pestañaSuma, text = 'Suma') # Añade un titulo a la pestaña e inicializa la ventana
  232. notebook.add(pestañaResta, text = 'Resta')
  233. notebook.add(pestañaMult, text = 'Multiplicación')
  234. notebook.add(pestañaDivn, text = 'División')
  235. notebook.add(pestañaRaizC, text = 'Raíz Cuadrada')
  236. notebook.add(pestañaElevacn, text = 'Elevación')
  237. notebook.add(pestañaCos, text = 'Coseno')
  238. notebook.add(pestañaSin, text = 'Seno')
  239. notebook.add(pestañaTan, text = 'Tangente')
  240. notebook.add(pestañaLog, text = 'Logaritmo')
  241. notebook.add(pestañaArctan, text = 'Arctangente')
  242. notebook.add(pestañaArCos, text = 'Coseno inverso')
  243. notebook.add(pestañaArSin, text = 'Seno inverso')
  244. notebook.add(pestañaSinh, text = 'Sin hiperbolico')
  245. notebook.add(pestañaCosh, text = 'Cos hiperbolico')
  246. notebook.add(pestañaTanh, text = 'Tan hiperbolico')
  247. notebook.add(pestañaArCosh, text = 'Arcos hiperbolico')
  248. notebook.add(pestañaArSinh, text = 'Arsin hiperbolico')
  249. notebook.add(pestañaArTanh, text = 'Artang hiperbolico')
  250.  
  251. ######################Pestaña 1 (Suma)######################
  252.  
  253. Label(pestañaSuma, text = "Introduce un número").grid(row=0, column=0)
  254. Entry(pestañaSuma, textvariable = num1).grid(row=0, column=1)
  255. Button(pestañaSuma, text='<< Put "total" here', command=put_in_num1).grid(row=0, column=2)
  256.        
  257. Label(pestañaSuma, text = "Introduce otro número").grid(row=1, column=0)
  258. Entry(pestañaSuma, textvariable = num2).grid(row=1, column=1)
  259. Button(pestañaSuma, text='<< Put "total" here', command=put_in_num2).grid(row=1, column=2)
  260.  
  261. Button(pestañaSuma, text = "Obtener resultado", fg = "#228B22", command = calcularSuma).grid(row=2, column=0)
  262.  
  263. ######################Pestaña 2 (Resta)######################
  264.  
  265. Label(pestañaResta, text = "Introduce un número").place(x = 20, y = 50)
  266. Entry(pestañaResta, textvariable = num1).place(x = 200, y = 50)
  267. Button(pestañaResta, text='<< Put "total" here', command=put_in_num1).place(x=400, y=50)
  268.  
  269. Label(pestañaResta, text = "Introduce otro número").place(x = 20, y = 80)
  270. Entry(pestañaResta, textvariable = num2).place(x = 200, y = 80)
  271. Button(pestañaResta, text='<< Put "total" here', command=put_in_num2).place(x=400, y=80)
  272.  
  273. Button(pestañaResta, text = "Obtener resultado", fg = "#228B22", command = calcularResta).place(x = 100, y = 150)
  274.  
  275. ######################Pestaña 3 (Multiplicación)######################
  276.  
  277. Label(pestañaMult, text = "Introduce un número").place(x = 20, y = 50)
  278. Entry(pestañaMult, textvariable = num1).place(x = 200, y = 50)
  279. Button(pestañaMult, text='<< Put "total" here', command=put_in_num1).place(x=400, y=50)
  280.  
  281. Label(pestañaMult, text = "Introduce otro número").place(x = 20, y = 80)
  282. Entry(pestañaMult, textvariable = num2).place(x = 200, y = 80)
  283.  
  284. Button(pestañaMult, text = "Obtener resultado", fg = "#228B22", command = calcularMultipl).place(x = 100, y = 150)
  285.  
  286. ######################Pestaña 4 (División)######################
  287.  
  288. Label(pestañaDivn, text = "Introduce un número").place(x = 20, y = 50)
  289. Entry(pestañaDivn, textvariable = num1).place(x = 200, y = 50)
  290. Button(pestañaDivn, text='<< Put "total" here', command=put_in_num1).place(x=400, y=50)
  291.  
  292. Label(pestañaDivn, text = "Introduce otro número").place(x = 20, y = 80)
  293. Entry(pestañaDivn, textvariable = num2).place(x = 200, y = 80)
  294.  
  295. Button(pestañaDivn, text = "Obtener resultado", fg = "#228B22", command = calcularDivn).place(x = 100, y = 150)
  296.  
  297. ######################Pestaña 5 (Raíz Cuadrada)#################
  298.  
  299. Label(pestañaRaizC, text = "Introduce un número").place(x = 20, y = 50)
  300. Entry(pestañaRaizC, textvariable = num1).place(x = 200, y = 50)
  301. Button(pestañaRaizC, text='<< Put "total" here', command=put_in_num1).place(x=400, y=50)
  302.  
  303. Button(pestañaRaizC, text = "Obtener resultado", fg = "#228B22", command = calcularRaizC).place(x = 100, y = 150)
  304.  
  305. ######################Pestaña 6 (Elevación)######################
  306.  
  307. Label(pestañaElevacn, text = "Introduce el numero a elevar").place(x = 20, y = 50)
  308. Entry(pestañaElevacn, textvariable = num1).place(x = 200, y = 50)
  309. Button(pestañaElevacn, text='<< Put "total" here', command=put_in_num1).place(x=400, y=50)
  310.  
  311. Label(pestañaElevacn, text = "Introduce el exponente").place(x = 20, y = 80)
  312. Entry(pestañaElevacn, textvariable = num2).place(x = 200, y = 80)
  313.  
  314. Button(pestañaElevacn, text = "Obtener resultado", fg = "#228B22", command = calcularElevacn).place(x = 100, y = 150)
  315.  
  316. ######################Pestaña 7 (Coseno)#########################
  317.  
  318. Label(pestañaCos, text = "Introduce un número").place(x = 20, y = 50)
  319. Entry(pestañaCos, textvariable = num1).place(x = 200, y = 50)
  320. Button(pestañaCos, text='<< Put "total" here', command=put_in_num1).place(x=400, y=50)
  321.  
  322. Button(pestañaCos, text = "Obtener resultado", fg = "#228B22", command = calcularCos).place(x = 100, y = 150)
  323.  
  324. ######################Pestaña 7 (Seno)###########################
  325.  
  326. Label(pestañaSin, text = "Introduce un número").place(x = 20, y = 50)
  327. Entry(pestañaSin, textvariable = num1).place(x = 200, y = 50)
  328. Button(pestañaSin, text='<< Put "total" here', command=put_in_num1).place(x=400, y=50)
  329.  
  330. Button(pestañaSin, text = "Obtener resultado", fg = "#228B22", command = calcularSin).place(x = 100, y = 150)
  331.  
  332. ######################Pestaña 7 (Tangente)#######################
  333.  
  334. Label(pestañaTan, text = "Introduce un número").place(x = 20, y = 50)
  335. Entry(pestañaTan, textvariable = num1).place(x = 200, y = 50)
  336. Button(pestañaTan, text='<< Put "total" here', command=put_in_num1).place(x=400, y=50)
  337.  
  338. Button(pestañaTan, text = "Obtener resultado", fg = "#228B22", command = calcularTan).place(x = 100, y = 150)
  339.  
  340. ######################Pestaña 7 (Logaritmo)######################
  341.  
  342. Label(pestañaLog, text = "Introduce un número").place(x = 20, y = 50)
  343. Entry(pestañaLog, textvariable = num1).place(x = 200, y = 50)
  344. Button(pestañaLog, text='<< Put "total" here', command=put_in_num1).place(x=400, y=50)
  345.  
  346. Button(pestañaLog, text = "Obtener resultado", fg = "#228B22", command = calcularLog).place(x = 100, y = 150)
  347.  
  348. #################################################################
  349.  
  350. Label(pestañaArctan, text = "Introduce un número").place(x = 20, y = 50)
  351. Entry(pestañaArctan, textvariable = num1).place(x = 200, y = 50)
  352. Button(pestañaArctan, text='<< Put "total" here', command=put_in_num1).place(x=400, y=50)
  353.  
  354. Button(pestañaArctan, text = "Obtener resultado", fg = "#228B22", command = calcularArctan).place(x = 100, y = 150)
  355.  
  356. ##################################################################
  357.  
  358. Label(pestañaSinh, text = "Introduce un número").place(x = 20, y = 50)
  359. Entry(pestañaSinh, textvariable = num1).place(x = 200, y = 50)
  360. Button(pestañaSinh, text='<< Put "total" here', command=put_in_num1).place(x=400, y=50)
  361.  
  362. Button(pestañaSinh, text = "Obtener resultado", fg = "#228B22", command = calcularSinh).place(x = 100, y = 150)
  363.  
  364. ##################################################################
  365.  
  366. Label(pestañaCosh, text = "Introduce un número").place(x = 20, y = 50)
  367. Entry(pestañaCosh, textvariable = num1).place(x = 200, y = 50)
  368. Button(pestañaCosh, text='<< Put "total" here', command=put_in_num1).place(x=400, y=50)
  369.  
  370. Button(pestañaCosh, text = "Obtener resultado", fg = "#228B22", command = calcularCosh).place(x = 100, y = 150)
  371.  
  372. ##################################################################
  373.  
  374. Label(pestañaArSin, text = "Introduce un número").place(x = 20, y = 50)
  375. Entry(pestañaArSin, textvariable = num1).place(x = 200, y = 50)
  376. Button(pestañaArSin, text='<< Put "total" here', command=put_in_num1).place(x=400, y=50)
  377.  
  378. Button(pestañaArSin, text = "Obtener resultado", fg = "#228B22", command = calcularArSin).place(x = 100, y = 150)
  379.  
  380. ##################################################################
  381.  
  382. Label(pestañaTanh, text = "Introduce un número").place(x = 20, y = 50)
  383. Entry(pestañaTanh, textvariable = num1).place(x = 200, y = 50)
  384. Button(pestañaTanh, text='<< Put "total" here', command=put_in_num1).place(x=400, y=50)
  385.  
  386. Button(pestañaTanh, text = "Obtener resultado", fg = "#228B22", command = calcularTanh).place(x = 100, y = 150)
  387.  
  388. ###################################################################
  389.  
  390. Label(pestañaArCosh, text = "Introduce un número").place(x = 20, y = 50)
  391. Entry(pestañaArCosh, textvariable = num1).place(x = 200, y = 50)
  392. Button(pestañaArCosh, text='<< Put "total" here', command=put_in_num1).place(x=400, y=50)
  393.  
  394. Button(pestañaArCosh, text = "Obtener resultado", fg = "#228B22", command = calcularArCosh).place(x = 100, y = 150)
  395.  
  396. ###################################################################
  397.  
  398. Label(pestañaArCos, text = "Introduce un número").place(x = 20, y = 50)
  399. Entry(pestañaArCos, textvariable = num1).place(x = 200, y = 50)
  400. Button(pestañaArCos, text='<< Put "total" here', command=put_in_num1).place(x=400, y=50)
  401.  
  402. Button(pestañaArCos, text = "Obtener resultado", fg = "#228B22", command = calcularArCos).place(x = 100, y = 150)
  403.  
  404. Label(pestañaArSinh, text = "Introduce un número").place(x = 20, y = 50)
  405. Entry(pestañaArSinh, textvariable = num1).place(x = 200, y = 50)
  406.  
  407. Button(pestañaArSinh, text = "Obtener resultado", fg = "#228B22", command = calcularArSinh).place(x = 100, y = 150)
  408.  
  409. ###################################################################
  410.  
  411. Label(pestañaArTanh, text = "Introduce un número").place(x = 20, y = 50)
  412. Entry(pestañaArTanh, textvariable = num1).place(x = 200, y = 50)
  413. Button(pestañaArTanh, text='<< Put "total" here', command=put_in_num1).place(x=400, y=50)
  414.  
  415. Button(pestañaArTanh, text = "Obtener resultado", fg = "#228B22", command = calcularArTanh).place(x = 100, y = 150)
  416.  
  417. ###################################################################
  418.  
  419. mainloop() # Crea el mainloop para la ventana
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement