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

╄→尐↘猪︶ㄣ 提交于 2019-12-13 09:50:19

问题


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'


回答1:


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.




回答2:


I would suggest a dictionary. A very simplified example

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

dict_of_instances={}
Ronald = Jurystudent(4.0,'Ronald', 'Colyar' , 6 , 10)
dict_of_instances["Ronald"]=Ronald
dict_of_instances["George"] = Jurystudent(3.2,'George', 'Washington' , 5 , 11)

## simulate "George" entered in Entry and
## "Ronald" entered as another, separate request
for student in ["George", "Ronald"]:
    print("\nPrinting info for", student)
    first=dict_of_instances[student].First
    last=dict_of_instances[student].Last
    print("     ", first, last)
    num_classes=dict_of_instances[student].Numberofclasses
    print("     ", num_classes, dict_of_instances[student].gradelvl)


来源:https://stackoverflow.com/questions/47193770/is-there-a-way-i-can-insert-ronald-into-studententry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!