如何在python中为桌面应用程序创建一个可编辑的表?

wixjitnu  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(301)

我正在尝试将此表作为桌面应用程序(而不是web)来执行。它应该是一个可编辑的表,允许用户引入自己的数据(输入),将其保存在数据库中(使用sqlite连接到数据库),如果数据库中已经有一些信息,则将其显示到表中,并且不允许用户更改。这使一个表像我想要的,但我不知道如何使用它的网格,因为它需要所有屏幕,我希望它下面的选择列表选项。非常感谢!我会让我的代码在下面。

from tkinter import ttk
from tkinter import *
from tkcalendar import Calendar, DateEntry

import sqlite3

class Client:

    dbName = 'Database.db' 

    def __init__(self, window):
        self.wind = window
        self.wind.title('Aplicación cliente')

    #Container
        frame = LabelFrame(self.wind, text = 'Registrar un nuevo cliente')
        frame.grid(row=0, column=0, columnspan=1, padx=20, pady=20)

    #Name input
        Label(frame, text = 'Nombre: ').grid(row=1, column=0)
        self.name = Entry(frame)
        self.name.focus
        self.name.grid(row=1, column=1, padx=10, pady=5)

    #Birth date input
        Label(frame, text = 'Fecha de nacimiento: ').grid(row=2, column=0)
        self.calendar = DateEntry(frame,width=30,bg="darkblue",fg="white")
        self.calendar.grid(row=2, column=1, padx=10, pady=5)

        Label(frame, text = 'Fecha de bautismo: ').grid(row=3, column=0)
        self.calendar = DateEntry(frame,width=30,bg="darkblue",fg="white")
        self.calendar.grid(row=3, column=1, padx=10, pady=5)

    #Select list
        var = StringVar()

        Label(frame, text = 'Grupo: ').grid(row=4, column=0)
        self.dropdownList = OptionMenu(frame, var, 'Hombre', 'Mujer', 'Anciano', 'Siervo ministerial', 'Precursor regular', 'Ungido', 'Otras ovejas')
        self.dropdownList.grid(row=4, column=1, padx=10, pady=5)

        rows = []
        for i in range(12):
            cols = []
            for j in range(7):
                e = Entry(relief=RIDGE)
                e.grid(row=i, column=j, sticky=NSEW)
                e.insert(END, '%d.%d' % (i, j))
                cols.append(e)
            rows.append(cols)

        def onPress():
            for row in rows:
                for col in row:
                    print(col.get())
                print

        Button(text='Fetch', command=onPress).grid()

    def run_query(self, query, parameters=()):
        with sqlite3.connect(self.dbName) as conn:
            cursor = conn.cursor()
            result = cursor.execute(query, parameters)
            conn.commit()
        return result

    def getClient(self):

        records = self.tree.get_children()
        for element in records:
            self.tree.delete(element)

        query = 'SELECT * FROM Client'
        dbRows = self.run_query(query)
        for row in dbRows:
            self.tree.insert('',0, text=row[1], values=row[2])

if __name__ == '__main__':
    window = Tk()
    application = Client(window)
    window.mainloop()
thtygnil

thtygnil1#

只是为了展示我的成就。让我知道您缺少什么,因为我不使用sqlite和tkcalendar。

import tkinter as tk

class Calendar(tk.Frame):
    def __init__(self, master):
        self.master = master
        self.master.title('Aplicación cliente')

        #Container
        frame = tk.LabelFrame(self.master, text = 'Registrar un nuevo cliente')
        frame.grid(row=0, column=0, columnspan=1, padx=20, pady=20)

        #Name input
        self.name = tk.Label(frame, text = 'Nombre: ')
        self.name.grid(row=1, column=0)

        self.name_e = tk.Entry(frame)
        self.name_e.focus()
        self.name_e.grid(row=1, column=1, padx=10, pady=5)

        #Birth date input
        self.calendar = tk.Label(frame, text = 'Fecha de nacimiento: ')
        self.calendar.grid(row=2, column=0)
        self.calendar_e = tk.Entry(frame,width=30,bg="darkblue",fg="white")
        self.calendar_e.grid(row=2, column=1, padx=10, pady=5)

        self.calendar2 = tk.Label(frame, text = 'Fecha de bautismo: ')
        self.calendar2.grid(row=3, column=0)
        self.calendar_e2 = tk.Entry(frame,width=30,bg="darkblue",fg="white")
        self.calendar_e2.grid(row=3, column=1, padx=10, pady=5)

        #Select list
        var = tk.StringVar()

        self.group = tk.Label(frame, text = 'Grupo: ')
        self.group.grid(row=4, column=0)
        self.dropdownList = tk.OptionMenu(frame, var, 'Hombre', 'Mujer',
                                       'Anciano', 'Siervo ministerial',
                                       'Precursor regular',
                                       'Ungido', 'Otras ovejas')
        self.dropdownList.grid(row=4, column=1, padx=10, pady=5)

        self.b = tk.Button(frame, text='gather', command=self.gather)
        self.b.grid(row=5, column=0, columnspan=2)

        #Tabel
        self.tabel = tk.Frame(self.master)
        self.tabel.grid(row=1, column=0)

        self.my_entries = []

        rows = []
        for i in range(12):
            cols = []
            for j in range(7):
                e = tk.Entry(self.tabel, relief='ridge')
                e.grid(row=i, column=j, sticky='NSEW')
                e.insert('end', '%d.%d' % (i, j))
                self.my_entries.append(e)
                cols.append(e)
            rows.append(cols)
    def gather(self):
        for i in self.my_entries:
            print(i.get())

if __name__ == '__main__':
    window = tk.Tk()
    application = Calendar(window)
    window.mainloop()

相关问题