Python多用户输入GUI扩展:新增输入字段及Treeview展示方法问询
Hey there! Let's break this down into straightforward steps since you're new to Python GUI development. I'll cover adding new input fields, updating your Treeview to show all columns, and making sure the data flows correctly from input to database to display.
First: Fix a Small Issue in Your Existing Code
Before we add new stuff, note that self.else is a Python keyword—you can't use it as a variable name! Let's rename that to something like self.other_info to avoid errors.
Step 1: Add New Input Fields
Let's say you want to add an "Email" field. Just copy the pattern you used for existing fields:
# Inside your __init__ method, after the "Something else" field Label(frame, text='Email:').grid(row=5, column=1) self.email = Entry(frame) self.email.grid(row=5, column=2)
You can repeat this for any other fields you need (like phone number, address, etc.)—just adjust the row number each time to avoid overlapping.
Step 2: Update the Treeview to Show All Columns
Your current Treeview is only set up to display 2 extra columns, but we need to expand it to match all your database fields. Here's how to update it:
# Replace your existing Treeview initialization with this self.tree = ttk.Treeview(height=10, columns=('last_name', 'age', 'other_info', 'email')) # UPDATED: Add all new columns here self.tree.grid(row=7, column=0, columnspan=2) # Set headings for every column self.tree.heading('#0', text='Name', anchor=W) self.tree.heading('last_name', text='Last Name', anchor=W) # UPDATED: Use column names instead of numbers self.tree.heading('age', text='Age', anchor=W) self.tree.heading('other_info', text='Something Else', anchor=W) self.tree.heading('email', text='Email', anchor=W) # NEW: Add heading for new field # Optional: Set column widths for better readability self.tree.column('#0', width=120) self.tree.column('last_name', width=120) self.tree.column('age', width=60) self.tree.column('other_info', width=150) self.tree.column('email', width=200)
Using named columns (like 'last_name') instead of numbers makes your code easier to read and maintain.
Step 3: Modify viewing_records() to Display All Data
Right now, you're only inserting row[1] (Name) and row[3] into the Treeview. We need to pass all the database columns to the value parameter:
def viewing_records(self): # Clear existing records (keep this part) records = self.tree.get_children() for element in records: self.tree.delete(element) # Fetch all data from the database query = 'SELECT * FROM pacienti ORDER BY nume DESC' db_rows = self.run_query(query) # Insert every row into the Treeview for row in db_rows: # row[0] is probably your ID (we don't show it), row[1] = Name, row[2] = Last Name, row[3] = Age, row[4] = Other Info, row[5] = Email self.tree.insert('', 0, text=row[1], value=(row[2], row[3], row[4], row[5])) # UPDATED: Add all row values here
Make sure the order of values matches the order of your Treeview columns!
Step 4: Update the adding() Method (Important!)
You didn't include your adding() method, but you need to modify it to insert the new fields into the database. For example:
def adding(self): # Get values from all input fields name = self.name.get() last_name = self.last.get() age = self.age.get() other_info = self.other_info.get() # Renamed from self.else email = self.email.get() # NEW: Get value from new field # Validate inputs (optional but good practice) if not name or not last_name: self.message['text'] = 'Name and Last Name are required!' return # Insert into database query = '''INSERT INTO pacienti (nume, prenume, varsta, altceva, email) VALUES (?, ?, ?, ?, ?)''' # UPDATED: Add new column to the query parameters = (name, last_name, age, other_info, email) # NEW: Include new field value self.run_query(query, parameters) # Reset input fields self.name.delete(0, END) self.last.delete(0, END) self.age.delete(0, END) self.other_info.delete(0, END) self.email.delete(0, END) # Refresh the Treeview self.viewing_records() self.message['text'] = 'Record added successfully!'
Final Check: Update Your Database Table
If you haven't already, you need to add the new columns to your pacienti table. You can run this SQL query once (either in a database tool or via your Python code):
ALTER TABLE pacienti ADD COLUMN email TEXT;
Repeat this for any other new fields you add (e.g., ALTER TABLE pacienti ADD COLUMN phone TEXT;).
That's it! Each time you add a new input field, just repeat these steps: add the Label/Entry, update the Treeview columns/headings, include the field in the adding() method, and add the column to your database table.
内容的提问来源于stack exchange,提问作者Ela Tina

