鱼C论坛

 找回密码
 立即注册
查看: 852|回复: 4

python中tkinter的主窗口和弹窗的显示

[复制链接]
发表于 2019-3-22 11:55:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请各位大佬指教,谢谢

目的:主窗口选择通讯类型,选定后显示弹窗内容,同时隐藏主窗口;
          弹窗关闭后,主窗口恢复。
问题:选择通讯类型后,主窗口的界面被弹窗的内容覆盖,弹窗内无任何内容

  1. import tkinter as tk
  2. from tkinter import ttk
  3. # import SocketTest
  4. # import time


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

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

  26.     def donothing(self):
  27.         pass

  28.     def newWindow(self, event_type):
  29.         if self.combobox.get() == '  TCP-Server':
  30.             mydialog = NewDialog(1)
  31.         elif self.combobox.get() == '  TCP-Client':
  32.             mydialog = NewDialog(2)
  33.         elif self.combobox.get() == '  UDP':
  34.             mydialog = NewDialog(3)
  35.         else:
  36.             return
  37.         self.withdraw()
  38.         self.wait_window(mydialog)
  39.         self.update()
  40.         self.deiconify()
  41.         return


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

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

  53.         self.localIp = tk.IntVar()
  54.         self.localPort = tk.IntVar()
  55.         self.destinationIp = tk.IntVar()
  56.         self.destinationPort = tk.IntVar()
  57.         self.sendEntry = tk.StringVar()
  58.         self.acceptEntry = tk.StringVar()
  59.         self.click = tk.StringVar()
  60.         self.connectStatus = 0

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

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

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

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

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

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

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

  107.         self.frame_top.grid_propagate(0)
  108.         self.frame_center_left.grid_propagate(0)
  109.         self.frame_center_right.grid_propagate(0)
  110.         self.frame_bottom.grid_propagate(0)


  111.     def socConnect(self):
  112.         pass


  113. if __name__ == '__main__':
  114.     L = TcpTool()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-22 12:29:23 | 显示全部楼层

  1. import tkinter as tk
  2. from tkinter import ttk
  3. # import SocketTest
  4. # import time


  5. class TcpTool(tk.Tk):
  6.     def __init__(self):
  7.         super().__init__()
  8.         self.modeSelected = tk.StringVar()
  9.         self.canvas = tk.Canvas(self, width=500, height=400)
  10.         self.background = tk.PhotoImage(file=r'1.gif')
  11.         self.dispalyBackground = self.canvas.create_image(250, 0, anchor='n', image=self.background)
  12.         self.combobox = ttk.Combobox(self, textvariable=self.modeSelected)
  13.         self.combobox['value'] = ('', '  TCP-Server', '  TCP-Client', '  UDP')
  14.         self.combobox.current(0)
  15.         # self.mode = tk.Label(text='Communicate Mode')

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

  26.     def donothing(self):
  27.         pass

  28.     def newWindow(self, event_type):
  29.         if self.combobox.get() == '  TCP-Server':
  30.             mydialog = NewDialog(1)
  31.         elif self.combobox.get() == '  TCP-Client':
  32.             mydialog = NewDialog(2)
  33.         elif self.combobox.get() == '  UDP':
  34.             mydialog = NewDialog(3)
  35.         else:
  36.             return
  37.         self.withdraw()
  38.         self.wait_window(mydialog)
  39.         self.update()
  40.         self.deiconify()
  41.         return


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

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

  53.         self.localIp = tk.IntVar()
  54.         self.localPort = tk.IntVar()
  55.         self.destinationIp = tk.IntVar()
  56.         self.destinationPort = tk.IntVar()
  57.         self.sendEntry = tk.StringVar()
  58.         self.acceptEntry = tk.StringVar()
  59.         self.click = tk.StringVar()
  60.         self.connectStatus = 0

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

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

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

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

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

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

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

  107.         self.frame_top.grid_propagate(0)
  108.         self.frame_center_left.grid_propagate(0)
  109.         self.frame_center_right.grid_propagate(0)
  110.         self.frame_bottom.grid_propagate(0)
  111.         self.mainloop()


  112.     def socConnect(self):
  113.         pass


  114. if __name__ == '__main__':
  115.     L = TcpTool()
复制代码

你都没有把你的frame放置到窗口里
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-22 14:29:35 | 显示全部楼层
塔利班 发表于 2019-3-22 12:29
你都没有把你的frame放置到窗口里

120-123行不是有放置吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-22 14:41:55 | 显示全部楼层
你grid来去谁知道是哪里grid,你最开始初始化的时候就没指定位置
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-24 16:21:32 | 显示全部楼层
塔利班 发表于 2019-3-22 14:41
你grid来去谁知道是哪里grid,你最开始初始化的时候就没指定位置

找了个办法,把这四个frame再放到一个总frame当中,然后就可以了。不过还是不明白为什么之前不可以。可以稍微解释一下吗,初始化的时候要先指定位置。 谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-14 17:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表