Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
104 views
in Technique[技术] by (71.8m points)

tkinter - Is There A Way i can insert 'Ronald' Into StudentEntry?

The Purpose is to enter in Ronald and get all of the information inside of the

Ronald Variable , using the method "INSERT" to make this happen , im stuck and

am looking on suggestions on how to do this ,

#the class/object
class Jurystudent:
    def __init__(self, gpa , First , Last, NUMOFCLASSES,gradelvl):
        self.gpa = gpa
        self. First = First
        self.Last = Last
        self.Numberofclasses = NUMOFCLASSES
        self.gradelvl = gradelvl
    def __str__(self):
        return str(self.gpa)+","+str(self.First)+ ","+str(self.Last)+","+str(self.Numberofclasses)+ ","+str(self.gradelvl)

#The function that inserts the result being searched into the result box

def insert():


Ronald = Jurystudent(4.0,'Ronald', 'Colyar' , 6 , 10)

print(Ronald)





#Gui
import tkinter
from tkinter import *
from tkinter import ttk
#Window 
root = Tk()

#Entry 
StudentEntry=ttk.Entry(root)
StudentEntry.grid(row = 1 , column = 0 , sticky =W)

#Button
SearchButton=ttk.Button(root , text ='Search')
SearchButton.grid(row = 1 , column= 1 , sticky =W)
SearchButton.bind('<Button-1>', insert)
#Result
Result  =ttk.Entry(root)

Result.grid(row= 1 , column = 2 , sticky= W )

this is a way i can search for the students at my school information.

ive tried using the .get function to get the value of the studententry entry

and use the insert function to place the value over to the Result entry , it

gives me a direct insertion of what i place in the studententry entry , for

example , if i were to put Ronald.gpa into the entry , I would like to get 4.0

in return into the result entry , instead im getting 'Ronald'

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok, I think I understand what you're trying to do here after reading this question and the one you asked earlier. You have a "database" (of sorts) of people who all have their own metadata and you want a way of displaying it in a tkinter window.

This is relatively simple and I think you're seriously, seriously over complicating things in your code.

The below example does what I think you need:

from tkinter import *

root = Tk()

class App:
    def __init__(self, root):
        self.root = root
        self.data = [{"Name": "Alpha", "Var1": "3", "Var2": "1", "Var3": "4"},
                 {"Name": "Beta", "Var1": "1", "Var2": "5", "Var3": "9"},
                 {"Name": "Charlie", "Var1": "2", "Var2": "6", "Var3": "6"}
            ]
        self.var = StringVar()
        self.var.trace("w", self.callback)
        self.entry = Entry(self.root, textvariable=self.var)
        self.entry.grid(row=0, column=0, columnspan=2)
        self.labels = [(Label(self.root, text="Name"),Label(self.root)),(Label(self.root, text="Var1"),Label(self.root)),(Label(self.root, text="Var2"),Label(self.root)),(Label(self.root, text="Var3"),Label(self.root))]
        for i in range(len(self.labels)):
            self.labels[i][0].grid(row = i+1, column=0)
            self.labels[i][1].grid(row = i+1, column=1)
    def callback(self, *args):
        for i in self.data:
            if i["Name"] == self.var.get():
                for c in self.labels:
                    c[1].configure(text=i[c[0].cget("text")])

App(root)
root.mainloop()

So let's break this down.

First of all, I'm using a different data structure. In the above example we have a list of dictionarys. This is a pretty typical way of getting a "database like" structure in Python.

I'm then using a different method for actually calling the search, opting to use a binding on the variable assigned to textvariable within the Entry widget. This means that every time the Entry widget is written to a variable is updated, whenever this variable is updated we get a callback to the callback() function, this section can be seen below:

        self.var = StringVar() #we generate a tkinter variable class here
        self.var.trace("w", self.callback) #we setup the callback here
        self.entry = Entry(self.root, textvariable=self.var) #and we assign the variable to the Entry widget here

Next up I essentially create 6 Label widgets in a 2 wide, 3 high grid. The Labels on the left act as an identifier and the Labels on the right are left blank for later. This is all pretty standard tkinter stuff which has been covered too many times on this site.

The last, and possibly hardest to understand section, is the actual callback, so let's look at it line-by-line:

for i in self.data:

This for loop simply cycles through every dictionary, assigning it to i.

    if i["Name"] == self.var.get():

This if statement says IF the value of the Entry widget IS EQUAL TO the value of the Name key in the dictionary we're currently looping through THEN True.

If the above check comes back as True we then loop through self.labels which contains the Label widgets we generated earlier.

                    c[1].configure(text=i[c[0].cget("text")])

Lastly, we set the second value in each tuple of the list (The blank Label widgets) to be the value of the dictionary we're looping through where the key matches the text of the identifier variable.

The above accomplishes what I think you want, you just need to amend your data to the format.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...