有没有办法在 StudentEntry 中插入“Ronald"?
目的是进入Ronald并获取里面的所有信息
The Purpose is to enter in Ronald and get all of the information inside of the
Ronald Variable,使用方法INSERT"来实现这一点,我被卡住了
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.
我尝试使用.get函数来获取studententry条目的值
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
例如,如果我将 Ronald.gpa 放入条目中,我希望得到 4.0
example , if i were to put Ronald.gpa into the entry , I would like to get 4.0
返回到结果条目,而不是我得到'Ronald'
in return into the result entry , instead im getting 'Ronald'
好的,我想我明白你在阅读这个问题和 你之前问过的那个.你有一个数据库"(各种各样的),他们都有自己的元数据,你想要一种在 tkinter 窗口中显示它的方法.
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()
那么让我们分解一下.
首先,我使用了不同的数据结构.在上面的例子中,我们有一个 dictionary
的 list
.这是在 Python 中获得类似数据库"结构的一种非常典型的方式.
First of all, I'm using a different data structure. In the above example we have a list
of dictionary
s. This is a pretty typical way of getting a "database like" structure in Python.
然后我使用不同的方法来实际调用搜索,选择在 Entry
中分配给 textvariable
的 variable
上使用绑定代码>小部件.这意味着每次将 Entry
小部件写入 variable
时都会更新,每当此 variable
更新时,我们都会收到 的回调>callback()
函数,这部分可以在下面看到:
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
接下来,我基本上在 2 宽 3 高的网格中创建了 6 个 Label
小部件.左边的 Label
作为标识符,右边的 Label
留空以备后用.这些都是非常标准的 tkinter 内容,在本网站上已多次提及.
Next up I essentially create 6 Label
widgets in a 2 wide, 3 high grid. The Label
s on the left act as an identifier and the Label
s 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:
这个 for
循环只是循环遍历每个字典,将其分配给 i
.
This for
loop simply cycles through every dictionary, assigning it to i
.
if i["Name"] == self.var.get():
这个 if
语句表示 IF Entry
小部件的值等于字典中 Name
键的值我们目前正在遍历 THEN True.
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.
如果上述检查结果为 True
,则我们循环遍历包含我们之前生成的 Label
小部件的 self.labels
.>
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")])
最后,我们将 list
(空白的 Label
小部件)的每个 tuple
中的第二个值设置为字典的值我们正在遍历键与标识符变量的文本匹配的位置.
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.