Text opens in wrong window tkinter

theoneandonly

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:

Image of where text is in the wrong window

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

Samuel T

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update text on a tkinter window

From Dev

After Windows 10 Creators Update, Remote Desktop opens in wrong window

From Dev

Why does time.sleep pause tkinter window before it opens

From Dev

How to run a cancellable function when a new Tkinter window opens

From Dev

Why does time.sleep pause tkinter window before it opens

From Dev

Tkinter; how to use scrollbar in a popup/top-level window that opens when a button is pressed in main root window

From Dev

Adding text to Tkinter Window - Formatting Specific String

From Dev

TKInter - The text in the label changes window's width

From Dev

Real Time text from a file on tkinter window

From Dev

Changing color of text in a tkinter window by using colorchooser in python 3 and tkinter

From Dev

Fancybox opens new window

From Dev

JOptionPane window opens in background

From Dev

New window opens incorrectly

From Dev

After text widget opens, tkinter GUI crashes/does not respond whenever its closed

From Dev

After text widget opens, tkinter GUI crashes/does not respond whenever its closed

From Dev

Chrome opens files with wrong application

From Dev

Bootstrap modal opens wrong one

From Dev

Chrome opens files with wrong application

From Dev

Submenu opens in wrong hover area

From Dev

angular uirouter opens wrong url

From Dev

Tkinter automatically opens my file

From Dev

showModalDialog; Opens a New Window in IE

From Dev

A blank black window opens in pygame

From Dev

A blank black window opens in pygame

From Dev

Onsubmit event opens new window

From Dev

Hotel software opens E-Mail Window in Outlook with generated text - how to change it automaticly to html or trigger a vba script?

From Dev

Tkinter: How can I save text entered in the Text Widget into a new window?

From Dev

Primefaces autocomplete menu opens in the wrong spot

From Dev

Universal Link opens wrong bundle ID

Related Related

  1. 1

    Update text on a tkinter window

  2. 2

    After Windows 10 Creators Update, Remote Desktop opens in wrong window

  3. 3

    Why does time.sleep pause tkinter window before it opens

  4. 4

    How to run a cancellable function when a new Tkinter window opens

  5. 5

    Why does time.sleep pause tkinter window before it opens

  6. 6

    Tkinter; how to use scrollbar in a popup/top-level window that opens when a button is pressed in main root window

  7. 7

    Adding text to Tkinter Window - Formatting Specific String

  8. 8

    TKInter - The text in the label changes window's width

  9. 9

    Real Time text from a file on tkinter window

  10. 10

    Changing color of text in a tkinter window by using colorchooser in python 3 and tkinter

  11. 11

    Fancybox opens new window

  12. 12

    JOptionPane window opens in background

  13. 13

    New window opens incorrectly

  14. 14

    After text widget opens, tkinter GUI crashes/does not respond whenever its closed

  15. 15

    After text widget opens, tkinter GUI crashes/does not respond whenever its closed

  16. 16

    Chrome opens files with wrong application

  17. 17

    Bootstrap modal opens wrong one

  18. 18

    Chrome opens files with wrong application

  19. 19

    Submenu opens in wrong hover area

  20. 20

    angular uirouter opens wrong url

  21. 21

    Tkinter automatically opens my file

  22. 22

    showModalDialog; Opens a New Window in IE

  23. 23

    A blank black window opens in pygame

  24. 24

    A blank black window opens in pygame

  25. 25

    Onsubmit event opens new window

  26. 26

    Hotel software opens E-Mail Window in Outlook with generated text - how to change it automaticly to html or trigger a vba script?

  27. 27

    Tkinter: How can I save text entered in the Text Widget into a new window?

  28. 28

    Primefaces autocomplete menu opens in the wrong spot

  29. 29

    Universal Link opens wrong bundle ID

HotTag

Archive