Code questions to increase the number using Python tkinter

Asked 2 years ago, Updated 2 years ago, 103 views

If you press the + button, the number increases and if you press the reset button, the Python gui code is initialized, but I don't know why there is no change even if I press the button. Can't it be applied to the key?

Below is the full text of the code.

from tkinter import *
from tkinter import ttk

incr=Tk()
incr.title("increasing_numbers")

global m_num
m_num=0
#Button Commands
def click(key):
    global m_num
    if key=='+':
        m_num+=1
    else:
        m_num=0


#Create result window
entry_val=StringVar(incr, value=m_num)
num_entry=ttk.Entry(incr, textvariable=entry_val, width=20)
num_entry.grid(row=0)

#+, create reset button

operator_list=['+','reset']

r=1
for btn_text in operator_list:
    def cmd(x=btn_text):
        click(x)
    Button(incr, text=btn_text, command=cmd).grid(row=r)
    r+=1

incr.mainloop()

python tkinter

2022-09-21 12:38

1 Answers

I made you change the number to the label instead of the result window, so I succeeded! The result window was increased by writing .insert, but every time I press the button, the numbers are printed together, so I give up... I even created a - button!

from tkinter import *
from tkinter import ttk

incr=Tk()
incr.title("increasing_numbers")

global m_num
m_num=0
#Button Commands
def click(key):
    global m_num
    if key=='+':
        m_num+=1
    elif key=='-':
        m_num-=1
    else:
        m_num=0
    num_var.set(m_num)

#Create Label
num_var=IntVar()
w=Label(incr, textvariable=num_var)
w.grid(row=0)

#+,-, create reset button
operator_list=['+','-','reset']

r=1
for btn_text in operator_list:
    def cmd(x=btn_text):
        click(x)
    ttk.Button(incr,text=btn_text,command=cmd).grid(row=r)
    r+=1


incr.mainloop()



2022-09-21 12:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.