Advertisement
furas

Python - tkinter - global

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