gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
write a fully function gui calculation using python code
- User
write a fully function gui calculation using python code
- Assistant
Sure, here's an example of a fully functional GUI calculator program using Python's Tkinter library:
import tkinter as tk def button_click(value): current_expression = expression_label["text"] if current_expression == "Error": clear_button_click() expression_label["text"] += value def equal_button_click(): current_expression = expression_label["text"] try: result = str(eval(current_expression)) expression_label["text"] = result except: expression_label["text"] = "Error" def clear_button_click(): expression_label["text"] = "" # Create the main window window = tk.Tk() window.title("Calculator") # Create the expression label expression_label = tk.Label(window, text="", anchor="e", relief="solid", width=26, height=2, font=("Arial", 20)) expression_label.grid(row=0, column=0, columnspan=4) # Create the number buttons for i in range(1, 10): button = tk.Button(window, text=str(i), command=lambda i=i: button_click(str(i)), width=5, height=2, font=("Arial", 20)) button.grid(row=3 - ((i - 1) // 3), column=(i - 1) % 3) # Create the zero button zero_button = tk.Button(window, text="0", command=lambda: button_click("0"), width=5, height=2, font=("Arial", 20)) zero_button.grid(row=4, column=1) # Create the operator buttons operator_symbols = ["+", "-", "*", "/"] row = 1 for symbol in operator_symbols: button = tk.Button(window, text=symbol, command=lambda symbol=symbol: button_click(symbol), width=5, height=2, font=("Arial", 20)) button.grid(row=row, column=3) row += 1 # Create the equal button equal_button = tk.Button(window, text="=", command=equal_button_click, width=5, height=2, font=("Arial", 20)) equal_button.grid(row=4, column=0) # Create the clear button clear_button = tk.Button(window, text="C", command=clear_button_click, width=5, height=2, font=("Arial", 20)) clear_button.grid(row=4, column=2) # Start the main event loop window.mainloop()This program creates a window with a label to display the expression, number buttons, operator buttons, equal button, and clear button. When a number or operator button is clicked, the corresponding value is added to the expression label. When the equal button is clicked, the expression is evaluated and the result is displayed in the expression label. The clear button clears the expression label.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.