2020. szeptember 9., szerda

Kalkulátor készítése Python és Java programozási nyelven




# Python program to  create a simple GUI  
# calculator using Tkinter 
  
# importál mindent a tkinter modulból
from tkinter import *
  
# globálisan deklarálja a kifejezés változót 
expression = "" 
  
  
# Funkció a kifejezés frissítésére 
# a szövegbeviteli mezőben
def press(num): 
    # rámutat a globális kifejezés változóra 
    global expression 
  
    #  karakterlánc összefűzése 
    expression = expression + str(num) 
  
    # update the expression by using set method 
    equation.set(expression) 
  
  
# Function to evaluate the final expression 
def equalpress(): 
    # Try and except statement is used 
    # for handling the errors like zero 
    # division error etc. 
  
    # Put that code inside the try block 
    # which may generate the error 
    try: 
  
        global expression 
  
        # eval function evaluate the expression 
        # and str function convert the result 
        # into string 
        total = str(eval(expression)) 
  
        equation.set(total) 
  
        # initialze the expression variable 
        # by empty string 
        expression = "" 
  
    # if error is generate then handle 
    # by the except block 
    except: 
  
        equation.set(" error ") 
        expression = "" 
  
  
# Function to clear the contents 
# of text entry box 
def clear(): 
    global expression 
    expression = "" 
    equation.set("") 
  
  
# Driver code 
if __name__ == "__main__": 
    # create a GUI window 
    gui = Tk() 
  
    # set the background colour of GUI window 
    gui.configure(background="light green") 
  
    # set the title of GUI window 
    gui.title("Simple Calculator") 
  
    # set the configuration of GUI window 
    gui.geometry("270x150") 
  
    # StringVar() is the variable class 
    # we create an instance of this class 
    equation = StringVar() 
  
    # create the text entry box for 
    # showing the expression . 
    expression_field = Entry(gui, textvariable=equation) 
  
    # grid method is used for placing 
    # the widgets at respective positions 
    # in table like structure . 
    expression_field.grid(columnspan=4, ipadx=70) 
  
    equation.set('enter your expression') 
  
    # create a Buttons and place at a particular 
    # location inside the root window . 
    # when user press the button, the command or 
    # function affiliated to that button is executed . 
    button1 = Button(gui, text=' 1 ', fg='black', bg='red', 
                     command=lambda: press(1), height=1, width=7) 
    button1.grid(row=2, column=0) 
  
    button2 = Button(gui, text=' 2 ', fg='black', bg='red', 
                     command=lambda: press(2), height=1, width=7) 
    button2.grid(row=2, column=1) 
  
    button3 = Button(gui, text=' 3 ', fg='black', bg='red', 
                     command=lambda: press(3), height=1, width=7) 
    button3.grid(row=2, column=2) 
  
    button4 = Button(gui, text=' 4 ', fg='black', bg='red', 
                     command=lambda: press(4), height=1, width=7) 
    button4.grid(row=3, column=0) 
  
    button5 = Button(gui, text=' 5 ', fg='black', bg='red', 
                     command=lambda: press(5), height=1, width=7) 
    button5.grid(row=3, column=1) 
  
    button6 = Button(gui, text=' 6 ', fg='black', bg='red', 
                     command=lambda: press(6), height=1, width=7) 
    button6.grid(row=3, column=2) 
  
    button7 = Button(gui, text=' 7 ', fg='black', bg='red', 
                     command=lambda: press(7), height=1, width=7) 
    button7.grid(row=4, column=0) 
  
    button8 = Button(gui, text=' 8 ', fg='black', bg='red', 
                     command=lambda: press(8), height=1, width=7) 
    button8.grid(row=4, column=1) 
  
    button9 = Button(gui, text=' 9 ', fg='black', bg='red', 
                     command=lambda: press(9), height=1, width=7) 
    button9.grid(row=4, column=2) 
  
    button0 = Button(gui, text=' 0 ', fg='black', bg='red', 
                     command=lambda: press(0), height=1, width=7) 
    button0.grid(row=5, column=0) 
  
    plus = Button(gui, text=' + ', fg='black', bg='red', 
                  command=lambda: press("+"), height=1, width=7) 
    plus.grid(row=2, column=3) 
  
    minus = Button(gui, text=' - ', fg='black', bg='red', 
                   command=lambda: press("-"), height=1, width=7) 
    minus.grid(row=3, column=3) 
  
    multiply = Button(gui, text=' * ', fg='black', bg='red', 
                      command=lambda: press("*"), height=1, width=7) 
    multiply.grid(row=4, column=3) 
  
    divide = Button(gui, text=' / ', fg='black', bg='red', 
                    command=lambda: press("/"), height=1, width=7) 
    divide.grid(row=5, column=3) 
  
    equal = Button(gui, text=' = ', fg='black', bg='red', 
                   command=equalpress, height=1, width=7) 
    equal.grid(row=5, column=2) 
  
    clear = Button(gui, text='Clear', fg='black', bg='red', 
                   command=clear, height=1, width=7) 
    clear.grid(row=5, column='1') 
  
    Decimal= Button(gui, text='.', fg='black', bg='red', 
                    command=lambda: press('.'), height=1, width=7) 
    Decimal.grid(row=6, column=0) 
    # start the GUI 
    gui.mainloop() 

----------------























Java programozási nyelven

package ptunes;
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
 
//import java.util.ActionEvent;
 
public class gui implements ActionListener {
 
public gui() {
 
}
 
public void actionPerformed (ActionEvent ae ){
 
// JOptionPane.showMessageDialog(ìHello is pressedî);
 
}
 
public static void main(String[] args) {
 
JFrame j = new JFrame("Buttons");
 
Container c = j.getContentPane();
//c.setLayout(new BorderLayout());
 
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
 
p1.setLayout(new GridLayout(4,4,4,4));
final JTextField t = new JTextField(100);
Font myFontSize = t.getFont().deriveFont(Font.BOLD,50f);
t.setFont(myFontSize);
c.add(t,BorderLayout.NORTH);
 
final JButton n1 = new JButton("1");
 
n1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String num1 = n1.getText();
String global = t.getText();
global = global.concat(num1);
t.setText(global);
 
}
 
});
 
final JButton n2 = new JButton("2");
n2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String num1 = n2.getText();
String global = t.getText();
global = global.concat(num1);
t.setText(global);
 
}
 
});
 
final JButton n3 = new JButton("3");
n3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String num1 = n3.getText();
String global = t.getText();
global = global.concat(num1);
t.setText(global);
 
}
 
});
 
final JButton n4 = new JButton("4");
n4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String num1 = n4.getText();
String global = t.getText();
global = global.concat(num1);
t.setText(global);
 
}
 
});
 
final JButton n5 = new JButton("5");
n5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String num1 = n5.getText();
String global = t.getText();
global = global.concat(num1);
t.setText(global);
 
}
 
});
 
final JButton n6 = new JButton("6");
n6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String num1 = n6.getText();
String global = t.getText();
global = global.concat(num1);
t.setText(global);
 
}
 
});
 
final JButton n7 = new JButton("7");
n7.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String num1 = n7.getText();
String global = t.getText();
global = global.concat(num1);
t.setText(global);
 
}
 
});
 
final JButton n8 = new JButton("8");
n8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String num1 = n8.getText();
String global = t.getText();
global = global.concat(num1);
t.setText(global);
 
}
 
});
 
final JButton n9 = new JButton("9");
n9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String num1 = n9.getText();
String global = t.getText();
global = global.concat(num1);
t.setText(global);
 
}
 
});
 
final JButton n10 = new JButton("0");
n10.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String num1 = n10.getText();
String global = t.getText();
global = global.concat(num1);
t.setText(global);
 
}
 
});
 
final JButton n11 = new JButton("+");
n11.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String num1 = n11.getText();
String global = t.getText();
global = global.concat(num1);
t.setText(global);
 
}
 
});
 
final JButton n12 = new JButton("-");
n12.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String num1 = n12.getText();
String global = t.getText();
global = global.concat(num1);
t.setText(global);
 
}
 
});
 
final JButton n13 = new JButton("*");
n13.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String num1 = n13.getText();
String global = t.getText();
global = global.concat(num1);
t.setText(global);
 
}
 
});
final JButton n14 = new JButton("/");
n14.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String num1 = n14.getText();
String global = t.getText();
global = global.concat(num1);
t.setText(global);
 
}
 
});
 
final JButton n15 = new JButton("=");
n15.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//String num1 = n15.getText();
String global = t.getText();
//global = global.concat(num1);
 
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
try {
String s = engine.eval(global).toString();
t.setText(s);
 
} catch (ScriptException e1) {
e1.printStackTrace();
}
 
}
 
});
 
final JButton n16 = new JButton("C");
n16.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//String num1 = n16.getText();
String global = t.getText();
global = null;
t.setText(global);
 
}
 
});
 
p1.add(n1);
p1.add(n2);
p1.add(n3);
p1.add(n4);
p1.add(n5);
p1.add(n6);
p1.add(n7);
p1.add(n8);
p1.add(n9);
p1.add(n10);
p1.add(n11);
p1.add(n12);
p1.add(n13);
p1.add(n14);
p1.add(n15);
p1.add(n16);
 
c.add(p1,BorderLayout.CENTER);
 
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setSize(400,400);
j.setVisible(true);
 
}
 
}


Nincsenek megjegyzések:

Megjegyzés küldése