如何在Tkinter中创建侧画布

2024-10-01 00:14:02 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

最终结果应该是这样的: enter image description here

我成功地做到了这一点:

enter image description here

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


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

我知道功能取决于很多东西,但我只是尝试在这个阶段创建基本布局,包括所有标题和边界

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

谢谢


Tags: oftheinselfmasterdefwidthkwargs
1条回答
网友
1楼 · 发布于 2024-10-01 00:14:02
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()

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

相关问题 更多 >