Dec-04-2024, 08:50 PM
Hello, Python Community
I'm working on a calculator with tkinter. Actually, I'm learning Python to be able to teach to a friend of mine, I'm a (.Net C#) programmer, she's not.
So I created 10 buttons for the digits, and their respective methods which write each digit in the textbox (Entry). Buuuuut ... I had the curious idea of sizing the buttons (in pixels), and searched the web, and found out that I had to add an (invisible 1x1) PhotoImage to the buttons to be able to size them in pixels (those are matters of Python I don't understand/like, that it is too elaborate in some aspects), because, as you may imagine, in the GUI calculator buttons need to be of a customized size. Well, it worked, but on the button I do this, then the associated method does not work any more, and I can't absolutely figure out why. That is, in my code button0 looks like desired, but does not print the 0 character in the textbox.
I will appreciate any help for solving this issue.
I attach the code by now.
Thank you!
Pablo
I'm working on a calculator with tkinter. Actually, I'm learning Python to be able to teach to a friend of mine, I'm a (.Net C#) programmer, she's not.
So I created 10 buttons for the digits, and their respective methods which write each digit in the textbox (Entry). Buuuuut ... I had the curious idea of sizing the buttons (in pixels), and searched the web, and found out that I had to add an (invisible 1x1) PhotoImage to the buttons to be able to size them in pixels (those are matters of Python I don't understand/like, that it is too elaborate in some aspects), because, as you may imagine, in the GUI calculator buttons need to be of a customized size. Well, it worked, but on the button I do this, then the associated method does not work any more, and I can't absolutely figure out why. That is, in my code button0 looks like desired, but does not print the 0 character in the textbox.
I will appreciate any help for solving this issue.
I attach the code by now.
Thank you!
Pablo
import tkinter as tk from tkinter import ttk class MainWindow(tk.Tk): global entry def __init__(self): global entry super().__init__() self.geometry("400x600+100+100") self.title("Calculadora Científica Python") entry = ttk.Entry(width=300, font=('Courier New', 20)) entry.place(x=10, y=10) pixel = tk.PhotoImage(width=1, height=1) button0 = tk.Button(text='0', width=60, height=40, image=pixel, compound='c', command=self.button0Press) button0.place(x=160, y=540) button1 = tk.Button(text='1', command=self.button1Press) button1.place(x=80, y=480) button2 = tk.Button(text='2', command=self.button2Press) button2.place(x=160, y=480) button3 = tk.Button(text='3', command=self.button3Press) button3.place(x=240, y=480) button4 = tk.Button(text='4', command=self.button4Press) button4.place(x=80, y=420) button5 = tk.Button(text='5', command=self.button5Press) button5.place(x=160, y=420) button6 = tk.Button(text='6', command=self.button6Press) button6.place(x=240, y=420) button7 = tk.Button(text='7', command=self.button7Press) button7.place(x=80, y=360) button8 = tk.Button(text='8', command=self.button8Press) button8.place(x=160, y=360) button9 = tk.Button(text='9', command=self.button9Press) button9.place(x=240, y=360) def button0Press(self): global entry entry.insert(tk.END, '0') def button1Press(self): global entry entry.insert(tk.END, '1') def button2Press(self): global entry entry.insert(tk.END, '2') def button3Press(self): global entry entry.insert(tk.END, '3') def button4Press(self): global entry entry.insert(tk.END, '4') def button5Press(self): global entry entry.insert(tk.END, '5') def button6Press(self): global entry entry.insert(tk.END, '6') def button7Press(self): global entry entry.insert(tk.END, '7') def button8Press(self): global entry entry.insert(tk.END, '8') def button9Press(self): global entry entry.insert(tk.END, '9') MainWindow().mainloop()