创建一个类似于 Tkinter 的表格 [英] Creating a table look-a-like Tkinter

查看:50
本文介绍了创建一个类似于 Tkinter 的表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在 Tkinter 中创建类似于表格的东西,但它不一定是一个.

I am looking to create something that resembles a table in Tkinter but it doesn't necessarily have to be one.

例如,我想创建标题Name1"、Name2"、Value",并在每个标题下方创建多个空白行.

I would like to create headers 'Name1', 'Name2', 'Value' for example and beneath each one I wish to have several blank rows.

然后我希望用我计算的值或名称的字符串值填充这些行(因此是标签).

These rows I then wish to fill in later (therefore a Label) with values I have calculated or string values for names.

对于Name2",我实际上希望整个列都是一个下拉菜单.我已经为它自己创建了代码,但不确定如何将其合并到这个表格"中.

For 'Name2' I would actually like for the entire column to be a drop down menu. I have already created the code for one on it's own but am not sure how to incorporate it into this 'table'.

例如,是否可以在 Label 小部件周围创建一个边框,使其看起来像一个表格"?

Is it for example possible to create a border around a Label widget so it looks like a 'table'?

任何关于在这种情况下可能发生的事情的指示将不胜感激.如果您需要我的任何代码来处理某事,请询问.谢谢!

Any pointers towards what would be possible in this situation would be very much appreciated. If you require any of my code to have a go at something, just ask. Thank you!

推荐答案

您遇到了什么问题?简单的解决方案是使用 grid 来布置小部件.您可以在每个单元格中放置任何类型的小部件.是的,标签可以有边框.不过,制作网格线的一种简单方法是在每个单元格周围使用填充,这样框架的颜色就会通过间隙显示出来.

What problem are you having? The simple solution is to lay out widgets using grid. You can put whatever type of widget you want in each cell. And yes, labels can have borders. Though, a simple way to do grid lines is to use a padding around each cell, so that the color of the frame will show through the gaps.

在框架中执行此操作.如果您需要能够滚动表格,请将框架放在画布内并添加滚动条.网上有很多关于如何使用画布创建可滚动框架的示例.

Do this in a frame. If you need to be able to scroll the table, put the frame inside a canvas and add scrollbars. There are examples all over the web for how to create a scrollable frame using a canvas.

这是一个非常简单的示例,它仅使用标签,不滚动.我将把您需要的确切实现作为练习留给读者.

Here's a really quick example that uses just labels, and doesn't scroll. I'll leave the exact implementation of what you need as an exercise for the reader.

import Tkinter as tk

class ExampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        t = SimpleTable(self, 10,2)
        t.pack(side="top", fill="x")
        t.set(0,0,"Hello, world")

class SimpleTable(tk.Frame):
    def __init__(self, parent, rows=10, columns=2):
        # use black background so it "peeks through" to 
        # form grid lines
        tk.Frame.__init__(self, parent, background="black")
        self._widgets = []
        for row in range(rows):
            current_row = []
            for column in range(columns):
                label = tk.Label(self, text="%s/%s" % (row, column), 
                                 borderwidth=0, width=10)
                label.grid(row=row, column=column, sticky="nsew", padx=1, pady=1)
                current_row.append(label)
            self._widgets.append(current_row)

        for column in range(columns):
            self.grid_columnconfigure(column, weight=1)


    def set(self, row, column, value):
        widget = self._widgets[row][column]
        widget.configure(text=value)

if __name__ == "__main__":
    app = ExampleApp()
    app.mainloop()

这篇关于创建一个类似于 Tkinter 的表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆