Добрый день всем. У меня есть два виджета в виде дерева (один на вкладке 1, другой на вкладке 2), связанных с базой данных mysql. После регистрации формы на вкладке 1 я хочу извлечь некоторые данные из древовидного представления на вкладке 1 в древовидное представление на вкладке 2 или автоматически заполнить древовидное представление на вкладке 2 выбранными элементами из древовидного представления вкладки 1, но я сохраняю при получении IndexError: строковый индекс вне диапазона, особенно с Student_ID. Я не знаю, почему это вне досягаемости. Пожалуйста, для цели вопроса, я удалил большинство кодов, просто чтобы сделать его маленьким, чтобы я мог разместить его здесь.
from tkinter import *
from tkinter import ttk
from tkinter.ttk import Combobox
import mysql.connector
root = Tk()
root.title('School Management System')
root.geometry("1400x700")
## ------------------------- TABS ---------------------------------------------------------------------------
tabcontrol = ttk.Notebook(root)
# tabcontrol.grid()
tabcontrol.pack(expand=1, fill='both')
tab1 = Frame(tabcontrol)
tabcontrol.add(tab1, text='Registration')
tab2 = Frame(tabcontrol)
tabcontrol.add(tab2, text='Class Score (30%)')
### ------------------------------------------------------------------------------------------------------------
### ------------------------------- TAB 1 (FRAMES 1) ----------------------------------------------------------------------
frame1 = Frame(tab1)
frame1.place(relx=0.03, rely=0.05, relheight=0.90, relwidth=0.45)
frame1.configure(relief=GROOVE)
frame1.configure(borderwidth="2")
text_surname = StringVar()
text_other_names = StringVar()
text_gender = StringVar()
class_ = StringVar()
text_age = StringVar()
text_student_id = StringVar()
text_full_name = StringVar()
text_date_of_vacation = StringVar()
text_position_in_class = StringVar()
label1 = Label(frame1, text="------------- REGISTRATION FORM -------------")
label1.place(relx=0.25, rely=0.15)
surname = Label(frame1, text="Surname")
surname.place(relx=0.03, rely=0.22)
txtsurname = Entry(frame1, width=20, textvariable=text_surname)
txtsurname.place(relx=0.15, rely=0.22)
othername = Label(frame1, text="Other Names")
othername.place(relx=0.03, rely=0.28)
txtothername = Entry(frame1, width=20, textvariable=text_other_names)
txtothername.place(relx=0.15, rely=0.28)
gender = Label(frame1, text="Gender")
gender.place(relx=0.03, rely=0.34)
txtgender = Combobox(frame1, textvariable=text_gender)
txtgender['values'] = ('Male', 'Female')
txtgender.place(relx=0.15, rely=0.34)
age = Label(frame1, text="Age")
age.place(relx=0.03, rely=0.46)
txtage = Entry(frame1, width=20, textvariable=text_age)
txtage.place(relx=0.15, rely=0.46)
####----------------------------------------End--------------------------------------------------
studentid = Label(frame1, text="Student ID")
studentid.place(relx=0.56, rely=0.28)
txtstudentid = Entry(frame1, textvariable=text_student_id)
txtstudentid.place(relx=0.68, rely=0.28)
Class = Label(frame1, text="Class")
Class.place(relx=0.60, rely=0.34)
txtClass = Combobox(frame1, textvariable=class_, justify=CENTER)
txtClass.place(relx=0.68, rely=0.34, relwidth=0.20)
txtClass.config(values=("Class 1", "Class 2", "Class 3", "Class 4", "Class 5", "Class 6",
"JHS 1", "JHS 2", "JHS 3", "Completed", "Stopped"))
## --------------------------------------------------------------------------------------------------------------
### ------------------------------- FRAMES 2 ----------------------------------------------------------------------
frame2 = Frame(tab1)
frame2.place(relx=0.45, rely=0.05, relheight=0.90, relwidth=0.50)
frame2.configure(relief=GROOVE)
frame2.configure(borderwidth="2")
frame_2 = Frame(tab1)
frame_2.place(relx=0.45, rely=0.12, relheight=0.83, relwidth=0.50)
frame_2.configure(relief=GROOVE)
frame_2.configure(borderwidth="2")
list_of_students = []
Class = Label(frame2, text="Class")
Class.place(relx=0.60, rely=0.03)
txtClass = Combobox(frame2, justify=CENTER)
txtClass.place(relx=0.66, rely=0.03, relwidth=0.20)
txtClass.config(values=("Class 1", "Class 2", "Class 3", "Class 4", "Class 5", "Class 6",
"JHS 1", "JHS 2", "JHS 3", "Completed", "Stopped"))
listofstudents = Label(frame2, text="List Of Students")
listofstudents.place(relx=0.20, rely=0.03)
## --------------------------------------------------------------------------------------------------------------
### ------------------------------- TAB 2 ---------------------------------------------------------------
frame3 = Frame(tab2)
frame3.place(relx=0.03, rely=0.02, relheight=0.20, relwidth=0.94)
frame3.configure(relief=GROOVE)
frame3.configure(borderwidth="2")
full_name = Label(frame3, text="Full Name ")
full_name.place(relx=0.03, rely=0.05)
txtfull_name = Entry(frame3, width=30, textvariable=text_full_name)
txtfull_name.place(relx=0.09, rely=0.05)
def set_label(name, index, mode):
text_full_name.set(text_surname.get() + ' ' + text_other_names.get())
text_surname.trace('w', set_label)
text_other_names.trace('w', set_label)
text_surname.set('')
text_other_names.set('')
age = Label(frame3, text="Age")
age.place(relx=0.03, rely=0.26)
txtage = Entry(frame3, width=20, textvariable=text_age)
txtage.place(relx=0.13, rely=0.26)
Class = Label(frame3, text="Class")
Class.place(relx=0.03, rely=0.48)
txtClass = Entry(frame3, textvariable=class_, justify=CENTER)
txtClass.place(relx=0.13, rely=0.48)
dateofvacation = Label(frame3, text="Date Of Vacation")
dateofvacation.place(relx=0.03, rely=0.70)
dateofvacation = Entry(frame3, textvariable=text_date_of_vacation)
dateofvacation.place(relx=0.13, rely=0.70)
gender = Label(frame3, text="Gender")
gender.place(relx=0.28, rely=0.05)
txtgender = Entry(frame3, textvariable=text_gender)
txtgender.place(relx=0.36, rely=0.05)
studentid = Label(frame3, text="Student ID")
studentid.place(relx=0.50, rely=0.05)
txtstudentid = Entry(frame3, textvariable=text_student_id)
txtstudentid.place(relx=0.60, rely=0.05)
position = Label(frame3, text="Position In Class")
position.place(relx=0.50, rely=0.24)
position = Entry(frame3, textvariable=text_position_in_class)
position.place(relx=0.60, rely=0.24)
frame4 = Frame(tab2)
frame4.place(relx=0.03, rely=0.20, relheight=0.75, relwidth=0.94)
frame4.configure(relief=GROOVE)
frame4.configure(borderwidth="2")
text_listStd = []
list_students = Label(frame4, text="LIST OF STUDENTS")
list_students.place(relx=0.03, rely=0.05)
list_students = Entry(frame4, textvariable=text_listStd)
list_students.place(relx=0.01, rely=0.09, relwidth=0.15)
#---------------------------------------------------------------------------------------------------------
con = mysql.connector.connect(host="localhost", user="root", password="billstone10", db="rock")
cur = con.cursor()
cur.execute("""CREATE TABLE IF NOT EXISTS info(ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, Student_ID varchar(20),
Surname varchar(20), Other_Names varchar(20), Full_Name varchar(20), Gender varchar(10), Class varchar(10), Age varchar(10))""")
con.commit()
con.close()
def addrec():
con = mysql.connector.connect(host="localhost", user="root", password="billstone10", db="rock")
cur = con.cursor()
cur.execute("INSERT INTO info(Student_ID, Surname, Other_Names, Full_Name, Gender, Class, Age) VALUES (%s, %s, %s, "
"%s, %s, %s, %s)", (text_student_id.get(), text_surname.get(), text_other_names.get(),
text_full_name.get(), text_gender.get(), class_.get(), text_age.get()))
con.commit()
fetch()
clearData()
con.close()
def fetch():
con = mysql.connector.connect(host="localhost", user="root", password="billstone10", db="rock")
cur = con.cursor()
cur.execute("SELECT Student_ID, Surname, Other_Names, Gender, Class, Age FROM info")
rows = cur.fetchall()
if len(rows) != 0:
student_list.delete(*student_list.get_children())
for row in rows:
student_list.insert("", END, values=row)
con.commit()
con.close()
def clearData():
txtsurname.delete(0, END)
txtothername.delete(0, END)
txtgender.delete(0, END)
txtage.delete(0, END)
txtstudentid.delete(0, END)
txtClass.delete(0, END)
def get_cursor(ev):
cursor_row = student_list.focus()
content = student_list.item(cursor_row)
row = content['values']
text_student_id.set(row[0]),
text_surname.set(row[1]),
text_other_names.set(row[2]),
text_gender.set(row[3]),
class_.set(row[4]),
text_age.set(row[5]),
scrollbary = Scrollbar(frame_2, orient=VERTICAL)
scrollbarx = Scrollbar(frame_2, orient=HORIZONTAL)
student_list = ttk.Treeview(frame_2, columns=("Student_ID", "Surname", "Other_Names", "Gender", "Class", "Age"),
selectmode="extended",
height=500, yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
scrollbary.config(command=student_list.yview)
scrollbary.pack(side=RIGHT, fill=Y)
scrollbarx.config(command=student_list.xview)
scrollbarx.pack(side=BOTTOM, fill=X)
student_list.heading('Student_ID', text="Student_ID", anchor=W)
student_list.heading('Surname', text="Surname", anchor=W)
student_list.heading('Other_Names', text="Other Names", anchor=W)
student_list.heading('Gender', text="Gender", anchor=W)
student_list.heading('Gender', text="Gender", anchor=W)
student_list.heading('Class', text="Class", anchor=W)
student_list.heading('Age', text="Age", anchor=W)
student_list.column('#0', stretch=NO, minwidth=0, width=0)
student_list.column('#1', stretch=NO, minwidth=0, width=80)
student_list.column('#1', stretch=NO, minwidth=0, width=80)
student_list.column('#2', stretch=NO, minwidth=0, width=120)
student_list.column('#3', stretch=NO, minwidth=0, width=80)
student_list.column('#4', stretch=NO, minwidth=0, width=150)
student_list.column('#5', stretch=NO, minwidth=0, width=120)
student_list.bind("<ButtonRelease-1>", get_cursor)
fetch()
student_list.place(relx=0.00, rely=0.00, relheight=0.97, relwidth=0.97)
#-------------------------------------------------------------------------------------------------
con = mysql.connector.connect(host="localhost", user="root", password="billstone10", db="rock")
cur = con.cursor()
cur.execute("""CREATE TABLE IF NOT EXISTS profile(ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, Student_ID varchar(20),
Date_of_Vacation varchar(10), Position_In varchar(10), FOREIGN KEY(Student_ID) REFERENCES info(Student_ID))""")
con.commit()
con.close()
def addrec2():
con = mysql.connector.connect(host="localhost", user="root", password="billstone10", db="rock")
cur = con.cursor()
cur.execute("INSERT INTO profile(Student_ID, Date_of_Vacation, Position_In) VALUES (%s, %s, %s)",
(text_student_id.get(), text_date_of_vacation.get(), text_position_in_class.get()))
con.commit()
fetch2()
con.close()
def fetch2():
con = mysql.connector.connect(host="localhost", user="root", password="billstone10", db="rock")
cur = con.cursor()
cur.execute("SELECT inn.Student_ID, inn.Full_Name, inn.Gender, inn.Class, inn.Age, Date_of_Vacation, Position_In "
"FROM info AS inn, profile")
rows = cur.fetchall()
if len(rows) != 0:
student_list_a.delete(*student_list_a.get_children())
for row in rows:
student_list_a.insert("", END, values=row)
con.commit()
con.close()
def get_cursor_a(ev):
cursor_row_a = student_list_a.focus()
content_a = student_list_a.item(cursor_row_a)
row_a = content_a['values']
text_student_id.set(row_a[0]),
text_full_name.set(row_a[1]),
text_gender.set(row_a[2]),
class_.set(row_a[3]),
text_age.set(row_a[4]),
scrollbary = Scrollbar(frame4, orient=VERTICAL)
scrollbarx = Scrollbar(frame4, orient=HORIZONTAL)
student_list_a = ttk.Treeview(frame4, columns=("Student_ID", "Surname", "Other_Names", "Gender", "Class", "Age"),
selectmode="extended", height=500, yscrollcommand=scrollbary.set,
xscrollcommand=scrollbarx.set)
scrollbary.config(command=student_list_a.yview)
scrollbary.pack(side=RIGHT, fill=Y)
scrollbarx.config(command=student_list_a.xview)
scrollbarx.pack(side=BOTTOM, fill=X)
student_list_a.heading('Student_ID', text="Student_ID", anchor=W)
student_list_a.heading('Surname', text="Surname", anchor=W)
student_list_a.heading('Other_Names', text="Other Names", anchor=W)
student_list_a.heading('Gender', text="Gender", anchor=W)
student_list_a.heading('Class', text="Class", anchor=W)
student_list_a.heading('Age', text="Age", anchor=W)
student_list_a.column('#0', stretch=NO, minwidth=0, width=0)
student_list_a.column('#1', stretch=NO, minwidth=0, width=80)
student_list_a.column('#1', stretch=NO, minwidth=0, width=80)
student_list_a.column('#2', stretch=NO, minwidth=0, width=120)
student_list_a.column('#3', stretch=NO, minwidth=0, width=80)
student_list_a.column('#4', stretch=NO, minwidth=0, width=150)
student_list_a.column('#5', stretch=NO, minwidth=0, width=120)
student_list_a.bind("<ButtonRelease-1>", get_cursor_a)
fetch()
fetch2()
student_list_a.place(relx=0.00, rely=0.00, relheight=0.97, relwidth=0.97)
#================================= Buttons ======================================================================
regbtn = Button(frame1, text='Register', command=addrec)
regbtn.place(relx=0.10, rely=0.94, relwidth=0.10)
#-----------------------------------------------------------------------------
regbtn = Button(frame4, text='Register', command=addrec2)
regbtn.place(relx=0.10, rely=0.94, relwidth=0.10)
root.mainloop()