Python Forum
function in new window (tkinter)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function in new window (tkinter)
#1
Hi I'm new to python
I'm trying to create a widget that performs a calculation on clicking a button, only I want the function to open in a new window. can anyone help me please? as you can see I've put the function into button 1 but I don't know how to open it in a new window.
Thanks

import tkinter
from colorama import Fore, Back
import math
u = 3 #underlay
f = 2.5 #fitting
import sys
import os
from tkinter import *
    


    
       

    

master=tkinter.Tk()
master.title("Carpet shop")
master.geometry("400x500")
master.configure(bg='peach puff')


def callback():
    w = float(input('carpet width:'))
    l = float(input('carpet length:'))
    p = float(input('carpet price:'))
        

    p1 = w*l*p #carpet only
    p4 = max(30,(w*l*f)) #fitting
    p2 = w*l*p + max(30,(w*l*f)) #carpet and fitting
    p3 = w*l*p + max(30,(w*l*f)) + w*l*u #carpet underlay and fitting
    p5 = w*l*p + max(30,(w*l*f)) + w*l*u + 10 #carpet, underlay, grips and fitting
    
    print( 'carpet only:£'"%.2f"%(p1))
    print( 'fitting:£'"%.2f"%(p4))
    print( 'carpet and fitting:£'"%.2f"%(p2))
    print( 'carpet, underlay and fitting:£'"%.2f"%(p3))
    print( 'carpet, underlay, grips and fitting:£'"%.2f"%(p5))
    print( '********************************************')
button1=tkinter.Button(master, text="Carpet (rooms)",width=20,height=5,highlightbackground='red',bd=5, fg='red',command = callback)
button1.grid(row=1,column=3)


        

button2=tkinter.Button(master, text="Carpet (stairs)",width=20,height=5,highlightbackground='red',bd=5, fg='red')
button2.grid(row=3,column=3)

button3=tkinter.Button(master, text="Vinyl",width=20,height=5,highlightbackground='red',bd=5, fg='blue')
button3.grid(row=5,column=3)

button4=tkinter.Button(master, text="Laminate (area)",width=20,height=5,highlightbackground='red',bd=5, fg='green')
button4.grid(row=7,column=3)

button5=tkinter.Button(master, text="Laminate (rooms)",width=20,height=5,highlightbackground='red',bd=5, fg='green')
button5.grid(row=9,column=3)

master.mainloop()
Reply
#2
Or do I need to create another frame and add it to the command for the button?
Any help would be appreciated thanks.
Reply
#3
you can use toplevel. If you only want the window around until a process is finished, you can destroy the window upon exit.

FYI: If you don't have a copy of the tkinter reference manual, you can get a copy here: http://reu.cct.lsu.edu/documents/Python_...kinter.pdf
Reply
#4
Hi Larz60
Thanks for your help and the PDF you provided. I've had a go with the top level and it creates a new window but i can't run the function in the window. Can you tell me where I'm going wrong please? Thanks.

def createNewWindow():
    newWindow = tk.Toplevel(master)
    newWindow.geometry("350x350")
    newWindow.title("Carpet (rooms)")

def callback():
    w = float(input('carpet width:'))
    l = float(input('carpet length:'))
    p = float(input('carpet price:'))
        

    p1 = w*l*p #carpet only
    p4 = max(30,(w*l*f)) #fitting
    p2 = w*l*p + max(30,(w*l*f)) #carpet and fitting
    p3 = w*l*p + max(30,(w*l*f)) + w*l*u #carpet underlay and fitting
    p5 = w*l*p + max(30,(w*l*f)) + w*l*u + 10 #carpet, underlay, grips and fitting
    
    print( 'carpet only:£'"%.2f"%(p1))
    print( 'fitting:£'"%.2f"%(p4))
    print( 'carpet and fitting:£'"%.2f"%(p2))
    print( 'carpet, underlay and fitting:£'"%.2f"%(p3))
    print( 'carpet, underlay, grips and fitting:£'"%.2f"%(p5))
    print( '********************************************')
    
button1=tkinter.Button(master, text="Carpet (rooms)",width=20,height=5,highlightbackground='red',bd=5, fg='red', command = lambda: [createNewWindow(), callback()])
                       
button1.grid(row=1,column=3)
Reply
#5
Please provide runnable code.
This should be relatively easy to do if using classes.
More difficult because of scope outside of class, because newWindow is not visible outside of the createNewWindow function.

You can make it global, but that's ugly and frowned upon.
Reply
#6
Ok sorry this is my full code. It opens a new window but the function is on the output and NOT in the new window. Not sure about the class, can you show me?

import tkinter
from colorama import Fore, Back
import math
import sys
import os
from tkinter import *
    


    
master = tkinter.Tk()
master.title("Carpet shop")
master.geometry("400x500")
master.configure(bg='peach puff')

def createNewWindow():
    newWindow = tk.Toplevel(master)
    newWindow.geometry("350x350")
    newWindow.title("Carpet (rooms)")

def callback():
    w = float(input('carpet width:'))
    l = float(input('carpet length:'))
    p = float(input('carpet price:'))
    u = 3 #underlay
    f = 2.5 #fitting
        

    p1 = w*l*p #carpet only
    p4 = max(30,(w*l*f)) #fitting
    p2 = w*l*p + max(30,(w*l*f)) #carpet and fitting
    p3 = w*l*p + max(30,(w*l*f)) + w*l*u #carpet underlay and fitting
    p5 = w*l*p + max(30,(w*l*f)) + w*l*u + 10 #carpet, underlay, grips and fitting
    
    print( 'carpet only:£'"%.2f"%(p1))
    print( 'fitting:£'"%.2f"%(p4))
    print( 'carpet and fitting:£'"%.2f"%(p2))
    print( 'carpet, underlay and fitting:£'"%.2f"%(p3))
    print( 'carpet, underlay, grips and fitting:£'"%.2f"%(p5))
    print( '********************************************')
    
button1=tkinter.Button(master, text="Carpet (rooms)",width=20,height=5,highlightbackground='red',bd=5, fg='red', command = lambda:[createNewWindow(), callback()])
                       
button1.grid(row=1,column=3)


        

button2=tkinter.Button(master, text="Carpet (stairs)",width=20,height=5,highlightbackground='red',bd=5, fg='red')
button2.grid(row=3,column=3)

button3=tkinter.Button(master, text="Vinyl",width=20,height=5,highlightbackground='red',bd=5, fg='blue')
button3.grid(row=5,column=3)

button4=tkinter.Button(master, text="Laminate (area)",width=20,height=5,highlightbackground='red',bd=5, fg='green')
button4.grid(row=7,column=3)

button5=tkinter.Button(master, text="Laminate (rooms)",width=20,height=5,highlightbackground='red',bd=5, fg='green')
button5.grid(row=9,column=3)

master.mainloop()
Reply
#7
I removed everything not needed to draw a new window for entering info to carpet a room.
You cannot use input to get info from this window or print to write to this window. You will have to create tk controls to do that stuff.
from tkinter import *

master = Tk()
master.title("Carpet shop")
 
def carpetRooms():
    newWindow = Toplevel(master)
    newWindow.geometry("350x350")
    newWindow.title("Carpet (rooms)")
    # Add code here to create Entries and labels and buttons
    # to enter the info needed to carpet a room.
     
Button(master, text="Carpet (rooms)", command = carpetRooms).grid(row=1,column=3)
 
master.mainloop()
You should avoid using pop-up windows when possible. Take a look at the tk notebook control which is a tabbed interface with multiple pages. You could replace your buttons with tabs, and the notebook pages take the place of your pop-up windows. This is the "approved" best way to make a GUI application with multiple views. I think the main reason for this is you can jump between views easily, maybe for reference or maybe cutting and pasting. This is tough to do if you have a pop-up view that goes away when another view is selected. And if you allow multiple pop-ups at the same time the screen gets cluttered.
Reply
#8
Thank you
I take a look at the tk notebook and see if I can run it with tabs. I'll let you know how it goes.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 262 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  [Tkinter] Tkinter Window Has no Title Bar gw1500se 4 315 Nov-07-2021, 05:14 PM
Last Post: gw1500se
  Creating a function interrupt button tkinter AnotherSam 2 462 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 492 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  tkinter get function finndude 2 932 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 1,758 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter
  "tkinter.TclError: NULL main window" Rama02 1 1,734 Feb-04-2021, 06:45 PM
Last Post: deanhystad
  Using Tkinter inside function not working Ensaimadeta 3 1,244 Dec-23-2020, 02:57 PM
Last Post: deanhystad
Star [Tkinter] How to perform math function in different page of Tkinter GUI ravaru 2 1,618 Oct-23-2020, 05:46 PM
Last Post: deanhystad
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 1,682 Oct-12-2020, 09:16 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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