如何在tkinter中将两个窗口彼此堆叠

时间:2018-12-12 11:18:09

标签: python user-interface tkinter multi-window tkinter-layout

我正在尝试设计一个GUI,以容纳用户可以与之交互的地图(四处移动,放大和缩小等),以及一些附加了不同功能的底部。

我希望它的工作方式是有一个仅包含地图的主窗口,以及位于其顶部的第二个窗口(代码中为“ topwin”),该窗口包含一个底部和底部各不相同的网格。标签。

有什么方法可以创建不涉及使用tk.Frame函数的第二个窗口?因为那是我目前所拥有的,当我运行代码时,它仅显示地图。

编辑:我收拾了topwin,但是我仍然没有得到想要的结果,我将附上我现在得到的图像以及我的目标是什么:

This is what my two windows stack on top of each other look like

This is what i want the GUI with two independent windows to look like

from tkinter import *
import tkinter as tk
from tkinter import messagebox
from PIL import Image
from PIL import ImageTk
from math import ceil

win = tk.Tk()
screen_width = win.winfo_screenwidth()
screen_height = win.winfo_screenheight()
win.geometry(f"{screen_width}x{screen_height}+0+0")
win.resizable(False, True)
win.title("Semi-Autonomous Fire Scout Drone Main Frame User Interface")

topwin = tk.Frame(win)
topwin.pack()

topwin.grid_columnconfigure(0, weight=1)
topwin.grid_columnconfigure(1, weight=1)

tenth_w = ceil(0.1*screen_width)
one_in_fifteen_w = ceil(0.015*screen_width)
one_in_fourfive_w = ceil(0.45*screen_width)
tenth_h = ceil(0.1*screen_height)
one_in_forty_h = ceil(0.4*screen_height)
one_in_fivefive_h = ceil(0.55*screen_height)


mapImg = img_resize("middle_earth_map.png", screen_width, screen_height)
logo_label = tk.Label(win, image=mapImg).pack()

def img_resize(file, width, height):
img = Image.open(file)
img = img.resize((width, height), Image.ANTIALIAS)
photoImg =  ImageTk.PhotoImage(img)
return photoImg


topLeftFrame = tk.Frame(topwin)
topLeftFrame.grid(row=0, column=0, padx=10, sticky="w")

homeImg = img_resize("home_icon.png", tenth_h, tenth_h)
homeb = tk.Button(topLeftFrame, image=homeImg, command=homeMenu, width=tenth_h, height= tenth_h)

rgbmenuImg = img_resize("rgb_icon.png", tenth_h, tenth_h)
rgbmenub = tk.Button(topLeftFrame, image=rgbmenuImg, command=rgbmenu, height=tenth_h, width=tenth_h)

thermalmenuImg = img_resize("thermal_icon.png", tenth_h, tenth_h)
thermalmenub = tk.Button(topLeftFrame, image=thermalmenuImg, command=thermalmenu, height=tenth_h, width=tenth_h)

homeb.grid(row=0, column=0, padx=10, pady=10)
rgbmenub.grid(row=0, column=1, padx=10, pady=10)
thermalmenub.grid(row=0, column=2, padx=10, pady=10)



topRightFrame = tk.Frame(topwin)
topRightFrame.grid(row=0, column=2, padx=10, sticky="ne")

settImg = img_resize("settings_icon.png", tenth_h, tenth_h)
settingb = tk.Button(topRightFrame, image=settImg, command=settings, height=tenth_h, width=tenth_h)

infoImg = img_resize("copyright_icon.png", tenth_h, tenth_h)
infob = tk.Button(topRightFrame, image=infoImg, command=copyright, height=tenth_h, width=tenth_h)

loginImg = img_resize("login_icon.png", tenth_h, tenth_h)
loginb = tk.Button(topRightFrame, image=loginImg, command=login, height=tenth_h, width=tenth_h)

exitImg = img_resize("exit_icon.png", tenth_h, tenth_h)
exitb = tk.Button(topRightFrame, image=exitImg, command=quit, height=tenth_h, width=tenth_h)

settingb.grid(row=0, column=0, padx=10, pady=10)
infob.grid(row=0, column=1, padx=10, pady=10)
loginb.grid(row=0, column=2,padx=10, pady=10)
exitb.grid(row=0, column=3, padx=10, pady=10)



rightFrame = tk.Frame(topwin)
rightFrame.grid(row=1, column=2, padx=10, pady=10, sticky="e")

tempMap = img_resize("temp_heat_map.png", one_in_fifteen_w, one_in_forty_h)
tempMaplab = tk.Label(rightFrame, image=tempMap).grid(row=0, column=0, padx=10, pady=10)



bottomFrame = tk.Frame(topwin, relief='solid', bd=2)
bottomFrame.grid(row=2, column=1, padx=10, pady=10, sticky="s")

info = """Drone Speed = XXX Km/h   Drone Distance = XXX m  Wind Direction = XX   Wind Force = XX Knots"""
info_botton = tk.Label(bottomFrame, justify=tk.CENTER, text=info, font = "arial 14 bold").grid(row=0, column=0, padx=10, pady=10)


win.mainloop()

0 个答案:

没有答案