I am trying to insert text into a new window that opens on a user's button click. However, when I try this, the textbox that I want to open in the new window opens up in the first window. See image below:
I have done my research and one answer I have come across is that I am sharing the 'Tk' widget too many times.
Here is my code
from tkinter import *
import tkinter as tk
root = tk.Tk()
text = Text(root, height=4, width=100)
text.pack()
text.insert(END, "The family car starting price is £24060 including VAT and
CO2 taxes")
text.insert(END, "\nThe sports car starting price is £30115 including VAT
and CO2 taxes")
text.insert(END, "\nThe suv car starting price is £36100 including VAT and
CO2 taxes")
def family_create_window():
window = tk.Toplevel(root)
window.title("family Exterior colour")
text = Text(root, height=4, width=100)
text.pack()
text.insert(END, "Hello")
def newwindow():
window = tk.Toplevel(root)
window.title("New window")
greyex = PhotoImage(file="vwfamilygrey.png")
greyexlabel = Button(window, image=greyex)
greyexbutton = Button(window, image=greyex, command=newwindow)
greyexbutton.pack()
window.mainloop()
familycar = PhotoImage(file = "VW family car.png")
familylabel = Button(root, image=familycar)
familybutton = Button(root, image=familycar, command=family_create_window)
familybutton.pack()
root.mainloop()
As soon as the user clicks on the image of the family car, what I want to happen is for a new window with images to open with the text 'hello' at the top of the window.
The 'greyex' is the exterior colour for additional help.
Any help appreciated. Thanks
Switch the parent widget of the Text
widget which says "hello" from root
to window
. This will make the Text widget appear on the new Window instead of the main window.
Example:
def family_create_window():
window = tk.Toplevel(root)
window.title("family Exterior colour")
text = Text(window, height=4, width=100) # Switch root to window
text.pack()
text.insert(END, "Hello")
Hope I helped
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments