saradis1
Coder
Hello. I would like to choose from the root1 the member and take it's ID of the member and put it in root window in the field "relation_with". I can't find how to go on or what to change to work.
Python:
from tkinter import *
from tkinter import messagebox
import tkinter as tk
import sqlite3
# Connection with my database
conn = sqlite3.connect('genealogy.db')
# Δημιουργία cursor για τα ερωτήματα
c = conn.cursor()
# Table creation
c.execute('''CREATE TABLE IF NOT EXISTS members
(id INTEGER PRIMARY KEY,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
date_of_birth TEXT NOT NULL,
relation TEXT NOT NULL,
relation_with INTEGER)''')
# Sending Data
conn.commit()
# function for member input
def insert_member():
# Get from the form
first_name = entry_first.get()
last_name = entry_last.get()
date_of_birth = entry_dob.get()
relation = relation_var.get()
relation_with = entry_relation_with.get()
# member's input in the database
c.execute(
'INSERT INTO members (first_name, last_name, date_of_birth, relation, relation_with) VALUES (?, ?, ?, ?, ?)',
(first_name, last_name, date_of_birth, relation, relation_with))
# Changes->sending
conn.commit()
# Clear my form after the input
clear_form()
# Succesful add
messagebox.showinfo(title="Επιτυχής", message="Η καταχώρησή σας ήταν επιτυχής")
root.destroy() #κλείσιμο του παραθύρου
# function Clear form
def clear_form():
entry_first.delete(0, 'end')
entry_last.delete(0, 'end')
entry_dob.delete(0, 'end')
entry_relation_with.delete(0, 'end')
# GUI
root = tk.Tk()
root.title("Γενεαλογικό Δέντρο-Εισαγωγή Μέλους")
#GUI Second Window for connection with "relationship_with"
root1= tk.Tk()
root1.title("Members to connect with relationship_with")
# root1.geometry("300x300") We can change it later...
#show all members in root1
# Create the listbox
listbox = tk.Listbox(root1)
listbox.pack()
# Retrieve all members from the database
c.execute("SELECT * FROM members")
members = c.fetchall()
# Loop through the members and add them to the listbox
for member in members:
listbox.insert(tk.END, member)
def update_entry_relation_with(member_id):
entry_relation_with.delete(0, tk.END) # Clear any existing value in the entry
entry_relation_with.insert(0, member_id) # Insert the selected member's ID in the entry
def select_member():
global member_id
selected_member = listbox.get(tk.ACTIVE) # Get the selected member
member_id = selected_member[0] # the first column contains the member's ID
# Δημιουργία Labels και entries (root)
lbl_id = tk.Label(root, text="ID:")
lbl_id.grid(row=0, column=0, padx=10, pady=5)
entry_id = tk.Entry(root, state='disabled')
entry_id.grid(row=0, column=1, padx=10, pady=5)
lbl_first = tk.Label(root, text="Name:")
lbl_first.grid(row=1, column=0, padx=10, pady=5)
entry_first = tk.Entry(root)
entry_first.grid(row=1, column=1, padx=10, pady=5)
lbl_last = tk.Label(root, text="Last name:")
lbl_last.grid(row=2, column=0, padx=10, pady=5)
entry_last = tk.Entry(root)
entry_last.grid(row=2, column=1, padx=10, pady=5)
lbl_dob = tk.Label(root, text="Date of birthday:")
lbl_dob.grid(row=3, column=0, padx=10, pady=5)
entry_dob = tk.Entry(root)
entry_dob.grid(row=3, column=1, padx=10, pady=5)
lbl_relation = tk.Label(root, text="Σχέση:")
lbl_relation.grid(row=4, column=0, padx=10, pady=5)
relation_var = StringVar(root)
relation_var.set("Relatioship:")
entry_relation = OptionMenu(root, relation_var, "Father", "Mother", "Child", "Grandpa")
entry_relation.grid(row=4, column=1, padx=10, pady=5)
lbl_relation_with = tk.Label(root, text="Relationship with the ID:")
lbl_relation_with.grid(row=5, column=0, padx=10, pady=5)
entry_relation_with = tk.Entry(root)
entry_relation_with.grid(row=5, column=1, padx=10, pady=5)
#buttons root
btn_submit = tk.Button(root, text="Καταχώρηση", command=insert_member)
btn_submit.grid(row=6, column=0, padx=10, pady=5)
btn_clear = tk.Button(root, text="Καθαρισμός φόρμας", command=clear_form)
btn_clear.grid(row=6, column=1, padx=10, pady=5)
#button root1
button_select_member = tk.Button(root1, text="Select member", command=select_member)
button_select_member.pack()
#Show The ID
c.execute('SELECT MAX(id) FROM members')
max_id = c.fetchone()[0]
if max_id is None:
max_id = 0
entry_id.config(state='normal')
entry_id.delete(0, 'end')
entry_id.insert(0, str(max_id+1))
entry_id.config(state='disabled')
#Start root
root.mainloop()
#Start root1
root1.mainloop()
#Close connection
conn.close()
Last edited by a moderator: