โปรแกรมคำนวณจารฐานข้อมูล SQlite 3

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderator: mindphp, ผู้ดูแลกระดาน

yellowgreen8404

โปรแกรมคำนวณจารฐานข้อมูล SQlite 3

โพสต์โดย yellowgreen8404 » 13/01/2019 1:35 pm

มือใหม่ฝึกทดลองกับ python รบกวนด้วยคะ

เราอยากลองทำโปรแกรมคำนวณขนาดยาในเด็กดู
โดยมีฐานข้อมูลของยาอยู่ด้วยเช่น ขนาดยา, ชื่อยา ฯลฯ
ซึ่งพอเรียกข้อมูลจากฐานข้อมูลออกมาเพื่อกดให้มันคำนวณมันจะ
ฟ้องว่า

runfile('C:/Users/Administrator/.spyder-py3/test20_sqlite3.py', wdir='C:/Users/Administrator/.spyder-py3')
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\Anaconda\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users/Administrator/.spyder-py3/test20_sqlite3.py", line 83, in get_selected_row
e6.insert(END, selected_tuple[6])
IndexError: tuple index out of range
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\Anaconda\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users/Administrator/.spyder-py3/test20_sqlite3.py", line 70, in get_selected_row
index = list1.curselection()[0]
IndexError: tuple index out of range


ไม่รู้ว่ามันผิดพลาดตรงใหน




โค้ด: เลือกทั้งหมด

# -*- coding: utf-8 -*-
"""
Created on Sun Jan 13 04:03:34 2019

@author: Administrator
"""

import sqlite3
from tkinter import*
from tkinter import ttk
from tkinter import messagebox

def calculate():
    def __init__(self):
        self.tk.call(self._w, 'insert', index, string)
        num2 = e2.get()
        num3 = e3.get()
        num4 = e4.get()
        num5 = e5.get()
       
        result = (int(num3)*int(num4))/(int(num2)*int(num5))
        e6.insert("", str(result))
       
def clearEntry():
    e1.delete(0, END)
    e2.delete(0, END)
    e3.delete(0, END)
    e4.delete(0, END)
    e5.delete(0, END)
    e6.delete(0, END)

class DB:
    def __init__ (self):
        self.conn = sqlite3.connect("pedia.db")
        self.cur = self.conn.cursor()
        self.cur.execute("CREATE TABLE IF NOT EXISTS drug (id INTEGER PRIMARY KEY,drug_name TEXT, strength INTEGER, dose INTEGER, child_weight INTEGER, frq INTEGER)")
        self.conn.commit()
   
    def __del__ (self):
        self.conn.close()
   
    def view(self):
        self.cur.execute("SELECT * From drug")
        rows = self.cur.fetchall()
        return rows
   
    def insert(self, drug_name, strength, dose, child_weight, frq):
        self.cur.execute("INSERT INTO drug VALUES (NULL,?,?,?,?,?)", (drug_name, strength, dose, child_weight, frq))
        self.conn.commit()
        self.view()
   
    def update(self, id,drug_name, strength, dose, child_weight, frq):
        self.cur.execute("UPDATE drug SET drug_name=?, strength=?, dose=?,child_weight=?, frq=? WHERE id=?", (drug_name, strength, dose, child_weight, frq))
        self.view()
   
    def delete(self, id):
        self.cur.execute("DELETE FROM drug WHERE id =?",(id,))
        self.conn.commit()
        self.view()
   
    def search(self, drug_name=""):
        self.cur.execute("SELECT * FROM drug WHERE drug_name=? ", (drug_name))
        rows = self.cur.fetchone()
        return rows
db = DB()


def get_selected_row(event):
    global selected_tuple
    index = list1.curselection()[0]
    selected_tuple = list1.get(index)
    e1.delete(0, END)
    e1.insert(END, selected_tuple[1])
    e2.delete(0, END)
    e2.insert(END, selected_tuple[2])
    e3.delete(0, END)
    e3.insert(END, selected_tuple[3])
    e4.delete(0, END)
    e4.insert(END, selected_tuple[4])
    e5.delete(0, END)
    e5.insert(END, selected_tuple[5])
    e6.delete(0, END)
    e6.insert(END, selected_tuple[6])

def view_command():
    list1.delete(0, END)
    for row in db.view():
        list1.insert(END, row)

def search_command():
    list1.delete(0, END)
    for row in db.search(drug_name_text.get()):
        list1.insert(END, row)

def add_command():
    db.insert(drug_name_text.get(), strength_text.get(), dose_text.get(), child_weight_text.get(), frq_text.get())
    list1.delete(0, END)
    list1.insert(END, (drug_name_text.get(), strength_text.get(), dose_text.get(), child_weight_text.get(), frq_text.get()))

def delete_command():
    db.delete(selected_tuple[0])


def update_command():
    db.update(selected_tuple[0], drug_name_text.get(), strength_text.get(), dose_text.get(), child_weight_text.get(), frq_text.get())

window = Tk()

window.title("Pediatric dose calculator")


def on_closing():
    dd = db
    if messagebox.askokcancel("Quit", "Do you want to quit?"):
        window.destroy()
        del dd

window.protocol("WM_DELETE_WINDOW", on_closing)  # handle window closing

l1 = Label(window, text="Drug Name")
l1.grid(row=0, column=0)

l2 = Label(window, text="Strength(mg/ml)")
l2.grid(row=0, column=2)

l3 = Label(window, text="dose(mg)")
l3.grid(row=1, column=0)

l4 = Label(window, text="Child Weidght (kg)")
l4.grid(row=1, column=2)

l5 = Label(window,  text="Frequency")
l5.grid(row=2, column=0)

l6 = Label(window, text ="ปริมาตรยาที่เด็กต้องได้(mL)")
l6.grid(row=3, column = 0)

e1 =ttk.Entry(window)
e2 =ttk.Entry(window)
e3 =ttk.Entry(window)
e4 =ttk.Entry(window)
e5 =ttk.Entry(window)
e6 =ttk.Entry(window)

drug_name_text = StringVar()
e1 = Entry(window, textvariable=drug_name_text)
e1.grid(row=0, column=1)

strength_text = StringVar()
e2 = Entry(window, textvariable=strength_text)
e2.grid(row=0, column=3)

dose_text = StringVar()
e3 = Entry(window, textvariable=dose_text)
e3.grid(row=1, column=1)

child_weight_text = StringVar()
e4 = Entry(window, textvariable= child_weight_text)
e4.grid(row=1, column=3)

frq_text = StringVar()
e5 = Entry(window, textvariable= frq_text)
e5.grid(row=2, column=1)

volume_text = StringVar()
e6 = Entry(window, textvariable= volume_text)
e6.grid(row=3, column=1)

list1 = Listbox(window, height=6, width=35)
list1.grid(row=4, column=0, rowspan=6, columnspan=2)

sb1 = Scrollbar(window)
sb1.grid(row=2, column=2, rowspan=6)

list1.configure(yscrollcommand=sb1.set)
sb1.configure(command=list1.yview)

list1.bind('<<ListboxSelect>>', get_selected_row)

b1 = Button(window, text="View all", width=12, command=view_command)
b1.grid(row=2, column=3)

b2 = Button(window, text="Search entry", width=12, command=search_command)
b2.grid(row=3, column=3)

b3 = Button(window, text="Add entry", width=12, command=add_command)
b3.grid(row=4, column=3)

b4 = Button(window, text="Update selected", width=12, command=update_command)
b4.grid(row=5, column=3)

b5 = Button(window, text="Delete selected", width=12, command=delete_command)
b5.grid(row=6, column=3)

b6 = Button(window, text="Close", width=12, command=window.destroy)
b6.grid(row=7, column=3)

b7 = Button(window, text="Calculate",width = 20, command=calculate)
b7.grid(row=10, column=0)

b8 = Button(window, text="Clear",width = 20 , command = clearEntry)
b8.grid(row=10, column =2)

window.mainloop()

yellowgreen8404

Re: โปรแกรมคำนวณจารฐานข้อมูลSQlite3

โพสต์โดย yellowgreen8404 » 14/01/2019 9:24 pm

โค้ด: เลือกทั้งหมด

# -*- coding: utf-8 -*-
"""
Created on Sun Jan 13 20:39:42 2019

@author: YellowGreen

Very Thankyou
1.Manjurul Hoque
and some of code from https://github.com/manjurulhoque/python-tkinter-projects
2.คู่มือการเขียน python โดย ณัฐวัตรคำภักดี
"""
import sqlite3
from tkinter import*
from tkinter import ttk
from tkinter import messagebox

def calculate():
    num1 = entDrugStrength.get()
    num2 = entDose.get()
    num3 = entFrq.get()
    num4 = entweight.get()
    result = (int(num2)*int(num4))/(int(num3)*int(num1))
    entVolume.insert("", str(result))

def clearEntry():
    entDrugStrength.delete(0, END)
    entDose.delete(0, END)
    entFrq.delete(0, END)
    entweight.delete(0, END)
    entVolume.delete(0, END)

class DB:
    def __init__ (self):
        self.conn = sqlite3.connect("pedia_cal2.db")
        self.cur = self.conn.cursor()
        self.cur.execute("CREATE TABLE IF NOT EXISTS pediadrug (id INTEGER PRIMARY KEY,drug_name TEXT, drug_strength INTEGER, dose INTEGER, frq INTEGER, childWeight INTEGER, volume FLOAT)")
        self.conn.commit()
   
    def __del__ (self):
        self.conn.close()
   
    def view(self):
        self.cur.execute("SELECT * From pediadrug")
        rows = self.cur.fetchall()
        return rows
   
    def insert(self, drug_name, drug_strength, dose, frq, childweight, volume):
        self.cur.execute("INSERT INTO pediadrug VALUES (NULL,?,?,?,?,?,?)", (drug_name, drug_strength, dose,frq, childweight, volume))
        self.conn.commit()
        self.view()
   
    def update(self, id,drug_name, drug_strength, dose,frq, childweight, volume):
        self.cur.execute("UPDATE pediadrug SET drug_name=?, drug_strength=?, dose=?,frq =?, childweight=?, volume=? WHERE id=?", (drug_name, drug_strength, dose,frq, childweight, volume ))
        self.view()
   
    def delete(self, id):
        self.cur.execute("DELETE FROM pediadrug WHERE id =?",(id,))
        self.conn.commit()
        self.view()
   
    def search(self, drug_name=""):
        self.cur.execute("SELECT * FROM pediadrug WHERE drug_name=? ", (drug_name))
        rows = self.cur.fetchone()
        return rows
db = DB()

   
   
def get_selected_row(event):
    global selected_tuple
    index = list1.curselection()[0]
    selected_tuple = list1.get(index)
    entDrugName.delete(0, END)
    entDrugName.insert(END, selected_tuple[1])
    entDrugStrength.delete(0, END)
    entDrugStrength.insert(END, selected_tuple[2])
    entDose.delete(0, END)
    entDose.insert(END, selected_tuple[3])

def view_command():
    list1.delete(0, END)
    for row in db.view():
        list1.insert(END, row)


def search_command():
    list1.delete(0, END)
    for row in db.search(drug_name.get(), drug_strength.get(), dose.get()):
        list1.insert(END, row)


def add_command():
    db.insert(drug_name.get(), drug_strength.get(), dose.get(), frq.get(), childweight.get(), volume.get())
    list1.delete(0, END)
    list1.insert(END, (drug_name.get(), drug_strength.get(), dose.get(), frq.get(), childweight.get(), volume.get()))


def delete_command():
    db.delete(selected_tuple[0])


def update_command():
    db.update(selected_tuple[0], drug_name.get(), drug_strength.get(), dose.get(),frq.get(), childweight.get(), volume.get())
   

mainfrm =Tk()
#mainfrm.geometry("800x600")
mainfrm.title("Pediatric dose Calc.")

def on_closing():
    dd = db
    if messagebox.askokcancel("Quit", "Do you want to quit?"):
        window.destroy()
        del dd

#Frame1-------------------------------------
lblFrm1 = ttk.LabelFrame(mainfrm, text="โปรแกรมคำนวณขนาดยาในเด็ก")
lblFrm1.pack(padx=10,pady=10,side="left")

lblDrugName = ttk.Label(lblFrm1, text="ชื่อสามัญทางยา").grid(column=0,row= 0,padx=5, sticky="e")
lblDrugStrength =ttk.Label(lblFrm1, text ="ความแรงของยา(mg/mL)").grid(column=0,row=1,padx=5, sticky="e")
lblDose= ttk.Label(lblFrm1, text="ขนาดยา(mg)").grid(column=0,row=2,padx=5, sticky="e")
lblFrq = ttk.Label(lblFrm1, text="ความถี่ในการกินยาต่อวัน(ครั้ง)").grid(column=0,row=3,padx=5, sticky="e")
lblweight= ttk.Label(lblFrm1, text="น้ำหนักเด็ก (kg)").grid(column=0, row =4,padx=5, sticky="e")
lblVolume=ttk.Label(lblFrm1, text="ปริมาตรยาที่ต้องได้รับ (mL)").grid(column=0, row =5,padx=5, sticky="e")

entDrugName = ttk.Entry(lblFrm1)
entDrugStrength =ttk.Entry(lblFrm1)
entDose=ttk.Entry(lblFrm1)
entFrq =ttk.Entry(lblFrm1)
entweight=ttk.Entry(lblFrm1)
entVolume=ttk.Entry(lblFrm1)

drug_name = StringVar()
entDrugName = Entry(lblFrm1, textvariable=drug_name)
entDrugName.grid(row=0, column=1)

drug_strength = StringVar()
entDrugStrength = Entry(lblFrm1, textvariable=drug_strength)
entDrugStrength.grid(row=1, column=1)

dose = StringVar()
entDose = Entry(lblFrm1, textvariable=dose)
entDose.grid(row=2, column=1)

frq = StringVar()
entFrq = Entry(lblFrm1, textvariable=frq)
entFrq.grid(row=3, column=1)

childweight = StringVar()
entweight = Entry(lblFrm1, textvariable=childweight)
entweight.grid(row=4, column=1)

volume = StringVar()
entVolume = Entry(lblFrm1, textvariable=volume)
entVolume.grid(row=5, column=1)


list1 = Listbox(lblFrm1, height=6, width=35)
list1.grid(column=1, row=6) 

sb1 = Scrollbar(lblFrm1)
sb1.grid(row=2, column=2, rowspan=6)

list1.configure(yscrollcommand=sb1.set)
sb1.configure(command=list1.yview)

list1.bind('<<ListboxSelect>>', get_selected_row)

btnEnter=ttk.Button(lblFrm1, text="Calculate",command=calculate).grid(column=0,row=7)
btnClear=ttk.Button(lblFrm1, text="Clear",command=clearEntry).grid(column=1,row=7)
#/Frame1----------------------------------------
#Frame2-----------------------------------------
lblFrm2 = ttk.Label(mainfrm, text="Micro Database Management",)
lblFrm2.pack(padx=10, pady =10,side="left" )

lblDb=ttk.Label(lblFrm2, text="บริหารฐานข้อมูล").grid(column=3,row=5,padx=5,sticky="e")

btnViewAll=ttk.Button(lblFrm2, text="View All",width=14, command=view_command).grid(column=3,row =6)
btnAddEntry=ttk.Button(lblFrm2, text ="Add Entry",width=14, command=add_command).grid(column=3,row=7)
btnUpdateEntry=ttk.Button(lblFrm2, text="UpdateSelected",width=14, command=update_command).grid(column=3,row=8)
btnDeleteEntry=ttk.Button(lblFrm2, text="DeleteSelected",width=14, command=delete_command).grid(column=3,row=9)

             
               
mainfrm.mainloop()

ภาพประจำตัวสมาชิก
thatsawan
PHP VIP Members
PHP VIP Members
โพสต์: 16899
ลงทะเบียนเมื่อ: 31/03/2014 10:02 am
ติดต่อ:

Re: โปรแกรมคำนวณจารฐานข้อมูลSQlite3

โพสต์โดย thatsawan » 14/01/2019 11:09 pm

ได้เเล้วหรือ

ภาพประจำตัวสมาชิก
meawkty
PHP Newbie
PHP Newbie
โพสต์: 1
ลงทะเบียนเมื่อ: 14/01/2019 2:57 pm

Re: โปรแกรมคำนวณจารฐานข้อมูลSQlite3

โพสต์โดย meawkty » 15/01/2019 11:25 am

thanks for news:mm


ย้อนกลับไปยัง

ผู้ใช้งานขณะนี้

กำลังดูบอร์ดนี้: 7 และ บุคคลทั่วไป 0 ท่าน