Python 如何在Tkinter中创建侧画布

Python 如何在Tkinter中创建侧画布,python,tkinter,canvas,Python,Tkinter,Canvas,我正在尝试创建“InventoryView”,它是地图的一个单独画布 虽然BasicMap和InventoryView都继承自AbstractGrid, 它们本身继承了Canvas,它们的设计目的不同。 BasicMap用于显示网格上实体的位置。 InventoryView用于显示玩家的库存和 允许玩家激活他们单击的项目 最终结果应该是这样的: 我成功地做到了这一点: 现在,我需要创建库存零件。 我正在尝试以下方法: import tkinter as tk from tkinter im

我正在尝试创建“InventoryView”,它是地图的一个单独画布

虽然BasicMap和InventoryView都继承自AbstractGrid, 它们本身继承了Canvas,它们的设计目的不同。 BasicMap用于显示网格上实体的位置。 InventoryView用于显示玩家的库存和 允许玩家激活他们单击的项目

最终结果应该是这样的:

我成功地做到了这一点:

现在,我需要创建库存零件。 我正在尝试以下方法:


import tkinter as tk
from tkinter import messagebox
from constants import *

class AbstractGrid(tk.Canvas):
    
    def __init__(self, master, rows, cols, width, height, **kwargs):
        """

        master: The window in which the grid should be drawn. 
        rows: The integer number of rows in the grid.
        cols: The integer number of columns in the grid.
        width: The width of the grid canvas (in pixels). 
        height: The height of the grid canvas (in pixels). 
        **kwargs: Any other additional named parameters appropriate to tk.Canvas.
        """
        
        super().__init__(master, width=width, height=height, **kwargs)

        self._rows = rows
        self._cols = cols
        self._width = width
        self._height = height
        # calculate the grid width and height
        self._grid_w = width / cols
        self._grid_h = height / rows

class InventoryView(AbstractGrid):
    """
    InventoryView is a view class which inherits from AbstractGrid and displays the items the
    player has in their inventory.
    """

    def __init__(self, master, rows, **kwargs):
        """
        master: The window in which the grid should be drawn.
        row: The  number of rows in the game map
        """
        super().__init__(master, rows, cols=2, width=INVENTORY_WIDTH, height=height, **kwargs)
        master.title("Inventory")
    def draw(self,inventory):
        """
        Draws the inventory label and current items with their remaining lifetime
        """
    def toggle_item_activation(self, pixel, inventory):
        """
        Activates or deactivates the item (if one exists) in the row containing the pixel
        """
我知道功能取决于很多东西,但我只是尝试在这个阶段创建基本布局,包括所有标题和边界

谁能帮我弄一下基本的布局图吗

谢谢


只需在窗口中创建两个框架,并根据需要添加小部件。从上面的代码中获取帮助。

只需在窗口中创建两个框架,它将为您创建一个分区,您可以执行所需操作。我认为在“InventoryView”中使用
Text
Treeview
比使用
AbstractGrid
更好。好的,在另一个框架中添加文本或任何您想添加的内容。这些框架只会创建窗口的一个分区
from tkinter import *

root = Tk()
frame1 = Frame(root, relief="sunken")
Label(frame1, text="This text is in frame 1").pack()
frame1.pack(side="left")

frame2 = Frame(root)
Label(frame2, text="This text is in frame 1").pack()
frame2.pack(side="right")

root.mainloop()