Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 文本在错误的窗口中打开tkinter_Python_Tkinter - Fatal编程技术网

Python 文本在错误的窗口中打开tkinter

Python 文本在错误的窗口中打开tkinter,python,tkinter,Python,Tkinter,我正在尝试将文本插入到一个新窗口中,该窗口在用户单击按钮时打开。但是,当我尝试此操作时,我想在新窗口中打开的文本框将在第一个窗口中打开。见下图: 我已经做了研究,我遇到的一个答案是我分享“Tk”小部件的次数太多了 这是我的密码 from tkinter import * import tkinter as tk root = tk.Tk() text = Text(root, height=4, width=100) text.pack() text.insert(END, "The fa

我正在尝试将文本插入到一个新窗口中,该窗口在用户单击按钮时打开。但是,当我尝试此操作时,我想在新窗口中打开的文本框将在第一个窗口中打开。见下图:

我已经做了研究,我遇到的一个答案是我分享“Tk”小部件的次数太多了

这是我的密码

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()
一旦用户点击家用汽车的图像,我想做的就是打开一个新的窗口,窗口顶部显示“hello”字样

“greyex”是额外帮助的外部颜色

谢谢你的帮助。谢谢

将表示“hello”的
文本
小部件的父小部件从
切换到
窗口
。这将使文本小部件显示在新窗口而不是主窗口上

例如:

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")

希望我有帮助

您可能需要将文本放置在正确的窗口中:将`text=text(root,height=4,width=100)
替换为
text=text(window,height=4,width=100)`Yep!我也试过了,效果非常好。谢谢很好,谢谢你!。消息现在显示在新窗口的顶部。再次感谢!