Python Forum
Anytime I press the button, the result is depicted
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Anytime I press the button, the result is depicted
#1
Hi,
how can I interrupt the displaying of the result, as soon as I press the button? The question relates to the first page, as soon as you press the Welcome-button. Then you have to input the lenght, the height and the wide. After that you use the calculate-button and the result is depicted on the left side. But if I click the button again, the result is depicted again and so on...
I know the rest of the code is unfinished, but for the moment, I just want to solve this small problem...

import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style

import tkinter as tk
from tkinter import ttk
from tkinter import *
LARGE_FONT = ("Verdana", 12)
style.use("ggplot")


    
class reverberationTime(tk.Tk):
    
    
    def __init__(self,*args,**kwargs):
    
       
        tk.Tk.__init__(self, *args, **kwargs)
        
        tk.Tk.wm_title(self, "reverberation time")
        tk.Tk.iconbitmap(self,"C:/Users/PC/Desktop/Icons/speaker.ico")
        
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)    
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
    
        self.frames = {}
        
        for F in (StartPage, PageOne, PageTwo, PageThree):
    
            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")
    
        self.show_frame(StartPage)

    def show_frame(self,cont):
        
        frame = self.frames[cont]
        frame.tkraise()
    
        
class StartPage(tk.Frame):
    
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text="reverberation time", font=LARGE_FONT)
        label.pack(pady = 10, padx = 10)
        
        button = ttk.Button(self, text="welcome!", 
                            command=lambda: controller.show_frame(PageOne)).pack()

        
class PageOne(tk.Frame):

    
    def __init__(self, parent, controller):
        
    
        tk.Frame.__init__(self, parent)
        NORMAL_FONT=("Arial",11)
        
        title = tk.Label(self, text="Please enter the dimensions", font=LARGE_FONT)
        title.pack(pady = 10, padx = 10)
       
        frame = Frame(self)
        frame.pack(pady=20)
        
        lenght = StringVar()
        wide = StringVar()
        height = StringVar()
        
        dimensions = Frame(frame)
        dimensions.pack(side='left', pady=5)
        
        entryfields = Frame(frame)
        entryfields.pack(side='right', pady=5)
        
        Label(dimensions, text="lenght:", font=NORMAL_FONT).pack(pady=3)
        Label(dimensions, text="wide:", font=NORMAL_FONT).pack(pady=4)
        Label(dimensions, text="height:", font=NORMAL_FONT).pack(pady=4)
        #Label(eingaben, text="Fensteranteil:", font=NORMAL_FONT).pack(pady=4)
        
       
    
        rl = Entry(entryfields, textvariable = lenght)
        rl.pack(pady=6)
        rw = Entry(entryfields, textvariable = wide)
        rw .pack(pady=6)
        rh = Entry(entryfields, textvariable = height)
        rh.pack(pady=6)
        #fa = Entry(entryfields)
        #fa.pack(pady=6)

        def calculate(carryOut):
            try:
                l = float(lenght.get())
                b = float(wide.get())
                h = float(height.get())
                v = l*b*h
                volume=Label(dimensions, text="volume: % .2f m³" % v).pack()
            
            except ValueError:
                tk.messagebox.showinfo('No valid input.','Please enter only numbers!',icon = 'warning')
                  
    
        #button3 = ttk.Button(self, text="berechnen",command=lambda: kalkulieren)
        button3 = ttk.Button(self, text="calculate")
        button3.pack()
        button3.bind("<Button-1>", calculate)
        
        button2 = ttk.Button(self, text="Page 2", 
                            command=lambda: controller.show_frame(PageTwo))
        button2.pack()
        
        button1 = ttk.Button(self, text="Start Page", 
                            command=lambda: controller.show_frame(StartPage))
        button1.pack()
        
        
   
    
class PageTwo(tk.Frame):
    
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        
       
        label = tk.Label(self, text="Page 2", font=LARGE_FONT)
        label.pack(pady = 10, padx = 10)

        
        button1 = ttk.Button(self, text="Start Page", 
                            command=lambda: controller.show_frame(StartPage)).pack()

        button2 = ttk.Button(self, text="Page 1", 
                            command=lambda: controller.show_frame(PageOne)).pack()
        

class PageThree(tk.Frame):
    
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        



app = reverberationTime()
app.mainloop()
Reply
#2
(Feb-23-2020, 10:31 PM)Jionni Wrote: But if I click the button again, the result is depicted again and so on...
On line 108 you create a new label every time you calculate. You need to create a textvariable and just update the text of that label every time it is calculate instead of creating a new label.

import tkinter as tk
from tkinter import ttk
from tkinter import *
LARGE_FONT = ("Verdana", 12)

 
 
     
class reverberationTime(tk.Tk):
     
     
    def __init__(self,*args,**kwargs):
     
        
        tk.Tk.__init__(self, *args, **kwargs)
         
        #tk.Tk.wm_title(self, "reverberation time")
        #tk.Tk.iconbitmap(self,"C:/Users/PC/Desktop/Icons/speaker.ico")
         
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)    
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
     
        self.frames = {}
         
        for F in (StartPage, PageOne, PageTwo, PageThree):
     
            frame = F(container, self)
 
            self.frames[F] = frame
 
            frame.grid(row=0, column=0, sticky="nsew")
     
        self.show_frame(StartPage)
 
    def show_frame(self,cont):
         
        frame = self.frames[cont]
        frame.tkraise()
     
         
class StartPage(tk.Frame):
     
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text="reverberation time", font=LARGE_FONT)
        label.pack(pady = 10, padx = 10)
         
        button = ttk.Button(self, text="welcome!", 
                            command=lambda: controller.show_frame(PageOne)).pack()
 
         
class PageOne(tk.Frame):
 
     
    def __init__(self, parent, controller):
         
     
        tk.Frame.__init__(self, parent)
        NORMAL_FONT=("Arial",11)
         
        title = tk.Label(self, text="Please enter the dimensions", font=LARGE_FONT)
        title.pack(pady = 10, padx = 10)
        
        frame = Frame(self)
        frame.pack(pady=20)
         
        self.lenght = StringVar()
        self.wide = StringVar()
        self.height = StringVar()
        self.v = StringVar()
         
        self.dimensions = Frame(frame)
        self.dimensions.pack(side='left', pady=5)
         
        entryfields = Frame(frame)
        entryfields.pack(side='right', pady=5)
         
        Label(self.dimensions, text="lenght:", font=NORMAL_FONT).pack(pady=3)
        Label(self.dimensions, text="wide:", font=NORMAL_FONT).pack(pady=4)
        Label(self.dimensions, text="height:", font=NORMAL_FONT).pack(pady=4)
        #Label(eingaben, text="Fensteranteil:", font=NORMAL_FONT).pack(pady=4)
         
        
     
        rl = Entry(entryfields, textvariable = self.lenght)
        rl.pack(pady=6)
        rw = Entry(entryfields, textvariable = self.wide)
        rw .pack(pady=6)
        rh = Entry(entryfields, textvariable = self.height)
        rh.pack(pady=6)
        #fa = Entry(entryfields)
        #fa.pack(pady=6)
                   
     
        #button3 = ttk.Button(self, text="berechnen",command=lambda: kalkulieren)
        button3 = ttk.Button(self, text="calculate")
        button3.pack()
        button3.bind("<Button-1>", self.calculate)
         
        button2 = ttk.Button(self, text="Page 2", 
                            command=lambda: controller.show_frame(PageTwo))
        button2.pack()
         
        button1 = ttk.Button(self, text="Start Page", 
                            command=lambda: controller.show_frame(StartPage))
        button1.pack()
        self.volume=Label(self.dimensions, textvariable=self.v).pack()

    def calculate(self, carryOut):
        try:
            l = float(self.lenght.get())
            b = float(self.wide.get())
            h = float(self.height.get())
            v = l*b*h
            self.v.set("volume: % .2f m³" % v)
         
        except ValueError:
            tk.messagebox.showinfo('No valid input.','Please enter only numbers!',icon = 'warning')
         
         
    
     
class PageTwo(tk.Frame):
     
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
         
        
        label = tk.Label(self, text="Page 2", font=LARGE_FONT)
        label.pack(pady = 10, padx = 10)
 
         
        button1 = ttk.Button(self, text="Start Page", 
                            command=lambda: controller.show_frame(StartPage)).pack()
 
        button2 = ttk.Button(self, text="Page 1", 
                            command=lambda: controller.show_frame(PageOne)).pack()
         
 
class PageThree(tk.Frame):
     
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
         
 
 
 
app = reverberationTime()
app.mainloop()
Recommended Tutorials:
Reply
#3
Yep, it works!

Thanks!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  .get() invoke after a button nested press iddon5 5 3,272 Mar-29-2021, 03:55 AM
Last Post: deanhystad
  tkinter touchscreen scrolling - button press makes unwanted scrolling nanok66 1 3,995 Dec-28-2020, 10:00 PM
Last Post: nanok66
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,009 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] how to input a random entry with each button press? nadavrock 1 6,399 Jun-17-2019, 05:28 AM
Last Post: Yoriz
  [Tkinter] Spawn sub-window with button press malonn 3 5,877 Oct-28-2018, 02:56 PM
Last Post: malonn
  [Tkinter] Updating Label After Button Press malonn 7 5,693 Aug-23-2018, 10:52 PM
Last Post: malonn
  [Tkinter] Problem with changing label text on button press xk2006x 1 5,574 Jun-02-2017, 06:00 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020