Python_tkinter关于主窗口和弹窗的控件的显示

请各位大佬指教,谢谢 :)

目的:主窗口选择通讯类型,选定后显示弹窗内容,同时隐藏主窗口;弹窗关闭后,主窗口恢复。

问题:选择通讯类型后,弹窗内无任何内容,关闭弹窗(还没添加弹窗的destroy,现在是直接右上角的×),发现主窗口的界面被弹窗的内容覆盖,没弄明白为什么会显示到主窗口去
: (

import tkinter as tk
from tkinter import ttk
# import SocketTest
# import time


class TcpTool(tk.Tk):
    def __init__(self):
        super().__init__()
        self.modeSelected = tk.StringVar()
        self.canvas = tk.Canvas(self, width=500, height=400)
        self.background = tk.PhotoImage(file=r'E:\picture3.gif')
        self.dispalyBackground = self.canvas.create_image(250, 0, anchor='n', image=self.background)
        self.combobox = ttk.Combobox(self, textvariable=self.modeSelected)
        self.combobox['value'] = ('', '  TCP-Server', '  TCP-Client', '  UDP')
        self.combobox.current(0)
        # self.mode = tk.Label(text='Communicate Mode')

        # self.displayBackground.pack()
        # self.combobox.place()
        self.combobox.bind("<<ComboboxSelected>>", self.newWindow)
        self.canvas.place(x=0, y=0)
        self.canvas.create_window(100, 80, width=100, height=20, window=self.combobox)
        # self.canvas.create_window(150,50,width=130,height=20,window=self.mode)
        self.title('Communicate Tools')
        self.geometry('500x300')
        self.resizable(False, False)
        self.mainloop()

    def donothing(self):
        pass

    def newWindow(self, event_type):
        if self.combobox.get() == '  TCP-Server':
            mydialog = NewDialog(1)
        elif self.combobox.get() == '  TCP-Client':
            mydialog = NewDialog(2)
        elif self.combobox.get() == '  UDP':
            mydialog = NewDialog(3)
        else:
            return
        self.withdraw()
        self.wait_window(mydialog)
        self.update()
        self.deiconify()
        return


class NewDialog(tk.Toplevel):
    def __init__(self, event):
        super().__init__()
        self.title('Communicate Tools')
        self.geometry('680x500')
        # self.resizable(False, False)
        self.wm_attributes('-topmost', 1)

        self.frame_top = tk.Frame(width=640, height=80, bg='grey')
        self.frame_center_left = tk.Frame(width=310, height=250, bg='blue')
        self.frame_center_right = tk.Frame(width=310, height=250, bg='yellow')
        self.frame_bottom = tk.Frame(width=640, height=100, bg='purple')

        self.localIp = tk.IntVar()
        self.localPort = tk.IntVar()
        self.destinationIp = tk.IntVar()
        self.destinationPort = tk.IntVar()
        self.sendEntry = tk.StringVar()
        self.acceptEntry = tk.StringVar()
        self.click = tk.StringVar()
        self.connectStatus = 0

        self.localIp.set('172.168.155.167')
        self.localPort.set('9000')
        self.destinationIp.set('172.168.155.166')
        self.destinationPort.set('9001')
        self.sendEntry.set('Enter data to send')
        # self.acceptEntry.set('data get from destination ')

        tk.Label(self.frame_top, text='Local IP:', width=10, height=1, font=('Arial', 12)
                 ).place(x=20, y=10)
        tk.Label(self.frame_top, text='Local Port:', width=10, height=1, font=('Arial', 12)).place(x=20, y=46)
        tk.Entry(self.frame_top, textvariable=self.localIp, font=('Arial', 13), width=14).place(x=125, y=10)
        tk.Entry(self.frame_top, textvariable=self.localPort, font=('Arial', 13), width=14).place(x=125, y=46)
        tk.Checkbutton(self.frame_top, text='连接', width=8, variable=self.click, font=('幼圆', 11),
                       cursor='arrow', selectcolor='red', relief='sunken', justify=tk.CENTER,
                       onvalue=1, offvalue=0, command=self.socConnect, indicatoron=False).place(x=560, y=10)
        self.socketStatus = tk.Label(text='', width=9, bg='red').place(x=572, y=56)

        self.remoteAddress = tk.Label(self.frame_top, text='Destination IP', width=15, height=1, font=('Arial', 12))
        self.remotePort = tk.Label(self.frame_top, text='Destination Port', width=15, height=1, font=('Arial', 12))
        self.enter_remoteAddress = tk.Entry(self.frame_top, textvariable=self.destinationIp,
                                            font=('Arial', 13), width=14)
        self.enter_remotePort = tk.Entry(self.frame_top, textvariable=self.destinationPort,
                                         font=('Arial', 13), width=14)

        self.remoteAddress.place(x=270, y=10)
        self.remotePort.place(x=270, y=46)
        self.enter_remoteAddress.place(x=420, y=10)
        self.enter_remotePort.place(x=420, y=46)

        tk.Entry(self.frame_center_left, textvariable=self.sendEntry, font=('Arial', 12)).place(x=10, y=220)
        self.sendAreaText = tk.Text(self.frame_center_left, width=33, height=10, font=('Arial', 11))
        self.sendAreaScroll = tk.Scrollbar(self.frame_center_left, orient='vertical', command=self.sendAreaText.yview)
        self.sendAreaText.configure(yscrollcommand=self.sendAreaScroll.set)
        self.send = tk.Label(self.frame_center_left, text='Send Area', width=10, height=1, font=('Arial', 12))
        self.sendAreaScroll.place(x=278, y=100)
        self.sendAreaText.place(x=10, y=35)
        self.send.place(x=107, y=5)

        # tk.Entry(self.frame_center_right, textvariable=self.acceptEntry, font=('Arial', 12)).place(x=10, y=10)
        self.acceptAreaText = tk.Text(self.frame_center_right, width=33, height=10, font=('Arial', 11))
        self.acceptAreaScroll = tk.Scrollbar(self.frame_center_right, orient='vertical', bd=50,
                                             command=self.acceptAreaText.yview)
        self.acceptAreaText.configure(yscrollcommand=self.acceptAreaScroll.set)
        self.accept = tk.Label(self.frame_center_right, text='Accept Area', width=10, height=1, font=('Arial', 12))
        self.acceptAreaScroll.place(x=278, y=100)
        self.acceptAreaText.place(x=10, y=35)
        self.accept.place(x=107, y=5)

        self.frame_top.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
        self.frame_center_right.grid(row=1, column=0, columnspan=2, padx=10, pady=5)
        self.frame_center_left.grid(row=1, column=2, columnspan=2, padx=5, pady=5)
        self.frame_bottom.grid(row=2, column=0, columnspan=3, padx=10, pady=10)

        self.frame_top.grid_propagate(0)
        self.frame_center_left.grid_propagate(0)
        self.frame_center_right.grid_propagate(0)
        self.frame_bottom.grid_propagate(0)


    def socConnect(self):
        pass


if __name__ == '__main__':
    L = TcpTool()

1个回答

可以通过把弹窗中的

        self.frame_top = tk.Frame(width=640, height=80, bg='grey')
        self.frame_center_left = tk.Frame(width=310, height=250, bg='blue')
        self.frame_center_right = tk.Frame(width=310, height=250, bg='yellow')
        self.frame_bottom = tk.Frame(width=640, height=100, bg='purple')

再放入一个大frame当中:
self.frame = tk.Frame(self, width=680, height=500)
即可解决
虽然不明白为什么之前会出现弹窗的内容跑到主窗口中了。。。。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐