首页 > 解决方案 > 如何在 Kivy python 代码中显示表格

问题描述

我想构建一个有管理员、代理和客户的应用程序。我想在 Kivy 屏幕应用程序上显示表格,并且应该显示的表格数据应该来自数据库。这是我的主要 python 文件代码

 from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import json
from kivy.uix.boxlayout import BoxLayout




Builder.load_file('design.kv')


class MainScreen(Screen):
    def goto_admin_page(self):
        self.manager.current = "adminfirst_screen"
    def goto_agent_page(self):
        pass
    def goto_customer_page(self):
        pass

class AdminScreenFirst(Screen):
    def go_to_adminsecond(self,uname,pword):
        with open("users.json") as file:
            users =json.load(file)
            if uname in users and users[uname]['password'] == pword:
                self.manager.current = "adminsecond_screen"
            else:
                self.ids.login_wrong.text = "Invalid Credentials.Please Contact the administrator"

class AdminScreenSecond(Screen):
    def go_to_adminpending(self):
        self.manager.current = "adminPending_screen"
    def go_to_adminapproved(self):
        self.manager.current = "adminsecond_screen"
    def go_to_adminrejected(self):
        self.manager.current = "adminsecond_screen"

class AdminPendingScreen(Screen):
    def display(self):
        pass

class Datatable(BoxLayout):
    pass
class RootWidget(ScreenManager):
    pass

class MainApp(App):
    def build(self):
        return RootWidget()


if __name__ == "__main__":
    MainApp().run()

这是我的 kivy 文件

<MainScreen>:
    GridLayout:
        cols:1
        GridLayout:
            cols:1
            padding:15,15
            spacing:20,20
            Label:
                text:"Login"
                font_size:"20sp"
            Button:
                text:"Admin"
                on_press : root.goto_admin_page()
            Button:
                text:"Agent"
                on_press : root.goto_agent_page()
            Button:
                text: "Customer"
                on_press:root.goto_customer_page()

<AdminScreenFirst>:
    GridLayout:
        cols:1
        padding:15,15
        spacing:20,20
        Label:
            text:"Admin Login"
            font_size:"20sp"
        TextInput:
            id:username
            hint_text:"Username"
        TextInput:
            id:password
            password:True
            hint_text:"Password"
        RelativeLayout:
            Button:
                text:"Login"
                on_press : root.go_to_adminsecond(root.ids.username.text,root.ids.password.text)
                size_hint :0.3,0.5
                pos_hint: {'center_x' : 0.5 , 'center_y' : 0.6}
        Label:
            id:login_wrong
            text:""
<AdminScreenSecond>:
    GridLayout:
        cols:1
        padding:15,15
        spacing:20,20
        Label:
            text:"Loan"
            font_size:"20sp"
        Button:
            text:"Pending Request"
            on_press:root.go_to_adminpending()
        Button:
            text:"Approved"
            on_press: root.go_to_adminapproved()
        Button:
            text:"Rejected"
            on_press: root.go_to_adminrejected()

<AdminPendingScreen>:
    GridLayout:
        cols:1
        padding:15,15
        spacing:20,20
        Label:
            text:"Loans"
        Button:
            text:"Display"
            on_press: root.display()
        Label:
            id:display
            text:""


<RootWidget>:
    MainScreen:
        name:"mainfirst_screen"
    AdminScreenFirst:
        name:"adminfirst_screen"
    AdminScreenSecond:
        name:"adminsecond_screen"
    AdminPendingScreen:
        name:"adminPending_screen"

我已经创建了数据库,并且我已经给出了创建数据库和表的命令,其中包含所有必需的信息
我已经构建了几个屏幕,但是我不知道如何在 kivy 代码中添加表以及我应该如何获取数据和检索数据来自应用程序并对其进行更改。我怎样才能做到这一点??我应该添加任何小部件或任何外部文件。

我会请求你帮我解决这个问题。先感谢您

标签: pythondatabasekivy

解决方案


推荐阅读