在Python/Beeware中双击表格时。

huangapple go评论60阅读模式
英文:

on_double_click on table in python/beeware

问题

I want to access data from a row of my table and display it in the terminal when that row is double-clicked.

import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW

class HelloWorld(toga.App):

    def startup(self):
        # Create a table with sample data
        data = [
            ['John', 'Doe', 30],
            ['Jane', 'Smith', 25],
            ['Alice', 'Johnson', 35]
        ]
        table = toga.Table(headings=['First Name', 'Last Name', 'Age'], data=data, on_double_click=self.clicked)

        # Create a main box and add the table to it
        main_box = toga.Box(children=[table], style=Pack(direction=COLUMN))

        self.main_window = toga.MainWindow(title=self.formal_name)
        self.main_window.content = main_box
        self.main_window.show()

    def clicked(self, table, row):
        print("Clicked row", row, "of table", table)

def main():
    app = HelloWorld()
    app.main_loop()

I have this output in the terminal, I need the line data and not just object numbers.

Clicked row <toga.sources.list_source.Row object at 0x000001E04101C750> of table Table:0x1e04100a850

英文:

I want to access data from a row of my table and display it in the terminal when that row is double-clicked.

import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW

class HelloWorld(toga.App):

    def startup(self):
        # Create a table with sample data
        data = [
            [&#39;John&#39;, &#39;Doe&#39;, 30],
            [&#39;Jane&#39;, &#39;Smith&#39;, 25],
            [&#39;Alice&#39;, &#39;Johnson&#39;, 35]
        ]
        table = toga.Table(headings=[&#39;First Name&#39;, &#39;Last Name&#39;, &#39;Age&#39;], data=data, on_double_click=self.clicked)

        # Create a main box and add the table to it
        main_box = toga.Box(children=[table], style=Pack(direction=COLUMN))

        self.main_window = toga.MainWindow(title=self.formal_name)
        self.main_window.content = main_box
        self.main_window.show()

    def clicked(self, table, row):
        print(&quot;Clicked row&quot;, row, &quot;of table&quot;, table)


def main():
    app = HelloWorld()
    app.main_loop()

I have this output in the terminal, I need the line data and not just object numbers.

Clicked row <toga.sources.list_source.Row object at 0x000001E04101C750> of table <Table:0x1e04100a850>

答案1

得分: 1

我尚未尝试过这个,也不愿意安装 toga 来尝试,但这是你需要的类型。请注意 main 中的更改以及clicked 方法的添加。请查阅文档以了解如何使用回调函数的参数。

import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW

class HelloWorld(toga.App):

    def startup(self):
        # 创建一个带有示例数据的表格
        data = [
            ['John', 'Doe', 30],
            ['Jane', 'Smith', 25],
            ['Alice', 'Johnson', 35]
        ]
        table = toga.Table(headings=['First Name', 'Last Name', 'Age'], data=data, on_double_click=self.clicked)

        # 创建一个主要容器并将表格添加到其中
        main_box = toga.Box(children=[table], style=Pack(direction=COLUMN))

        self.main_window = toga.MainWindow(title=self.formal_name)
        self.main_window.content = main_box
        self.main_window.show()

    def clicked(self, table, row):
        print("点击了表格的第", row, "行", table)

def main():
    app = HelloWorld()
    app.main_loop()
英文:

I have not tried this, and I'm not willing to install toga to do so, but here is the kind of thing you need. Note the changes in main and the addition of the clicked method. Please check the documentation to learn how to use the parameters to the callback.

import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW


class HelloWorld(toga.App):

    def startup(self):
        # Create a table with sample data
        data = [
            [&#39;John&#39;, &#39;Doe&#39;, 30],
            [&#39;Jane&#39;, &#39;Smith&#39;, 25],
            [&#39;Alice&#39;, &#39;Johnson&#39;, 35]
        ]
        table = toga.Table(headings=[&#39;First Name&#39;, &#39;Last Name&#39;, &#39;Age&#39;], data=data, on_double_click=self.clicked)

        # Create a main box and add the table to it
        main_box = toga.Box(children=[table], style=Pack(direction=COLUMN))

        self.main_window = toga.MainWindow(title=self.formal_name)
        self.main_window.content = main_box
        self.main_window.show()

    def clicked(self, table, row):
        print( &quot;Clicked row&quot;, row, &quot;of table&quot; table )


def main():
    app = HelloWorld()
    app.main_loop()

huangapple
  • 本文由 发表于 2023年6月29日 22:55:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定