英文:
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 = [
['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>
答案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 = [
['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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论