need help with tkinter | Sololearn: Learn to code for FREE!

+7

need help with tkinter

I need to make a program in which pressing the "+" button adds 1 to the number shown on the screen. And since I'm new to graphical interfaces, I don't know how to do it. I leave what I did so far: from tkinter import * def sumar(): resultado.set(+1) root=Tk() root.title("Contador") resultado=IntVar() Label(root,text="Contador:").grid(row=0,column=0) Entry(root,justify=CENTER,state="disable",textvariable=resultado).grid(row=0,column=1) Button(root,text="+",command=sumar).grid(row=0,column=2) root.mainloop()

5/7/2021 8:31:37 PM

Miss Yamileth

5 Answers

New Answer

+10

I don't know if you mean something like this: import tkinter as tk class VentanaBoton(): def __init__(self): self.root = tk.Tk() self.text = tk.IntVar() self.text.set("1") self.label = tk.Label(self.root, textvariable=self.text) self.button = tk.Button(self.root,text="+",command=lambda: self.sumar()) self.button.pack() self.label.pack() self.root.mainloop() def sumar(self): self.text.set(self.text.get()+1) app=VentanaBoton()

+2

from tkinter import * def sumar(label): text = label["text"] text = text.replace("Contador:", "") try: text = int(text) except: text = 0 text = "Contador:" + str(text + 1) label.configure(text=text) root=Tk() root.title("Contador") resultado=IntVar() label = Label(root,text="Contador:").grid(row=0,column=0) Entry(root,justify=CENTER,state="disable",textvariable=resultado).grid(row=0,column=1) Button(root,text="+",command=sumar).grid(row=0,column=2) root.mainloop()

+2

from tkinter import * num = 0 def sumar(): global num num += 1 resultado.set(num) root=Tk() root.title("Contador") resultado=IntVar() Label(root,text="Contador:").grid(row=0,column=0) Entry(root,justify=CENTER,state="disable",textvariable=resultado).grid(row=0,column=1) Button(root,text="+",command=sumar).grid(row=0,column=2) root.mainloop()

+2

Martin Mejia don't post irrelevant answers to the post's

0