我正在使用Tkinter,并希望将前三个tk.Entry()
从Entry转换为下拉菜单,使用户可以选择百分比(0.01%,0.02%,...等)
'''This simple App calculates a (component grade) for student. The way it works like the following:
the students enter the Value of school, Qiyas, and Comprehensive (in percent out of 100%), Then
the students enter the school, Qiyas, and Comprehensive grades also (in percent out of 100%),
the calculation of the component grade like this:
((school grade*school value)+(Qiyas*Qiyas value)+(Comprehensive grade*(Comprehensive value)))
'''
from PIL import Image, ImageTk
import datetime
import tkinter as tk
window = tk.Tk()
window.geometry("350x400")
window.title("Calculate the final grade")
#I would like to convert the following tk.Entry() to DropDown Menu
school_value_entry= tk.Label(text="Enter the Value of school in % (ex. 0.50)")
school_value_entry.grid(column = 0, row=1)
school_value_entry=tk.Entry()
school_value_entry.grid(column = 2 , row=1)
Qiyas_value_entry= tk.Label(text="Enter the Value of Qiyas in % (ex. 0.25)")
Qiyas_value_entry.grid(column = 0, row=2)
Qiyas_value_entry=tk.Entry()
Qiyas_value_entry.grid(column = 2 , row=2)
Comprehensive_value_entry= tk.Label(text="Enter the Value of Comprehensive in %(ex. 0.30)")
Comprehensive_value_entry.grid(column = 0, row=3)
Comprehensive_value_entry=tk.Entry()
Comprehensive_value_entry.grid(column = 2 , row=3)
school= tk.Label(text="High School Grade")
school.grid(column = 0, row=4)
school_entry=tk.Entry()
school_entry.grid(column = 2 , row=4)
Qiyas= tk.Label(text="Qiyas Grade")
Qiyas.grid(column = 0, row=5)
Qiyas_entry=tk.Entry()
Qiyas_entry.grid(column = 2 , row=5)
Comprehensive= tk.Label(text="Comprehensive Grade ")
Comprehensive.grid(column = 0, row=6)
Comprehensive_entry=tk.Entry()
Comprehensive_entry.grid(column = 2 , row=6)
#Calculating the grade& get the value from the user
def calculate_grade():
print(school_entry.get())
print(Qiyas_entry.get())
print(Comprehensive_entry.get())
Mohammed = person('Your Component Grade is:', float(school_entry.get()),
float(Qiyas_entry.get()),
float(Comprehensive_entry.get()),
float(school_value_entry.get()),
float(Qiyas_value_entry.get()),
float(Comprehensive_entry.get()))
text_answer= tk.Text(master = window, height= 20, width= 30)
text_answer.grid(column= 2, row= 7)
answer_text= "{} {} ".format(Mohammed.name,"%.2f" % Mohammed.grade())
text_answer.insert(tk.END, answer_text)
claculate_butten= tk.Button(text="Calculate Grade", command= calculate_grade)
claculate_butten.grid(column = 2, row=7)
class person:
def __init__(self, name, a, f, g, x, y, z):
self.name= name
self.a= school_value_entry
self.y= Qiyas_value_entry
self.z= Comprehensive_value_entry
self.x= school
self.f= Qiyas
self.g= Comprehensive
def grade(self):
a= school_value_entry.get()
f= Qiyas_value_entry.get()
g= Comprehensive_value_entry.get()
x= school_entry.get()
y= Qiyas_entry.get()
z= Comprehensive_entry.get()
''' the calculation of the component grade works like this:
((school grade*school value)+(Qiyas*Qiyas value)+(Comprehensive grade*(Comprehensive value))) '''
grade = (float(school_entry.get())*float(school_value_entry.get()))+(float(Qiyas_entry.get())*float(Qiyas_value_entry.get()))+(float(Comprehensive_entry.get())*float(Comprehensive_value_entry.get()))
return grade
image = Image.open('/Users/macbookpro/Downloads/logo.jpg')
image.thumbnail((300, 400), Image.ANTIALIAS)
photo= ImageTk.PhotoImage(image)
label_image= tk.Label(image = photo)
label_image.grid(column=2 , row=0)
#input("Press enter to exit ;)")
window.mainloop()
答案 0 :(得分:0)
好好看看你的代码有几个问题。类person
类接缝毫无意义,因为在函数中可以完成所有操作。您分配变量但是再次使用.get()
方法而不是使用您创建的变量。因此,代码中的某些内容毫无意义。
话虽如此,我在前3个输入框的位置添加了一些下拉菜单框。我设置它的方式可能并不完美但它是一个开始,你应该能够改进代码以满足你的需求。
首先,我创建了一个字典,我们将使用下拉框来提供每个百分比的值,并创建了一些我们稍后将使用的变量。
drop_choice = {}
s1 = "0.01"
q1 = "0.01"
c1 = "0.01"
其次,我创建了一些函数,用于在每次相应的下拉变化时更新s1,q1和c1。
def callback1(value):
global s1
s1 = value
def callback2(value):
global q1
q1 = value
def callback3(value):
global c1
c1 = value
第三,我创建了一个函数,将所有值添加到drop_choice字典中。
我并不完全知道你想要的所有值,所以我创建了0.01-1.00。
def setup_percentage():
new_percent = 0.00
for i in range(100):
new_percent += 0.01
new_value = float('{0:.2f}'.format(new_percent))
drop_choice[str(new_value)] = new_value
setup_percentage()
我为每个下拉菜单创建了StringVar()
。
school_value_entry_variable = tk.StringVar()
Qiyas_value_entry_variable = tk.StringVar()
Comprehensive_value_entry_variable = tk.StringVar()
最后一个重大变化是创建下拉菜单。
option1 = tk.OptionMenu(window, school_value_entry_variable, *drop_choice, command = callback1)
school_value_entry_variable.set('0.01')
option1.grid(column = 2 , row=1)
之后我更改了一些公式,以使用我最初创建的s1,q1和c1变量。
通过所有这些信息,您应该能够更好地了解如何设置这些下拉菜单。
以下是代码的复制粘贴版本,其中包含添加的下拉列表:
我将代码留在了你的前3个小工具的位置。我只是将它们评论出来并添加了下拉菜单。
import tkinter as tk
window = tk.Tk()
window.geometry()
window.title("Calculate the final grade")
s1 = "0.01"
q1 = "0.01"
c1 = "0.01"
def callback1(value):
global s1
s1 = value
def callback2(value):
global q1
q1 = value
def callback3(value):
global c1
c1 = value
drop_choice = {}
school_value_entry_variable = tk.StringVar()
Qiyas_value_entry_variable = tk.StringVar()
Comprehensive_value_entry_variable = tk.StringVar()
def setup_percentage():
new_percent = 0.00
for i in range(100):
new_percent += 0.01
new_value = float('{0:.2f}'.format(new_percent))
drop_choice[str(new_value)] = new_value
setup_percentage()
print(drop_choice)
#I would like to convert the following tk.Entry() to DropDown Menu
school_value_entry= tk.Label(window, text="Enter the Value of school in % (ex. 0.50)")
school_value_entry.grid(column = 0, row=1)
# school_value_entry=tk.Entry()
# school_value_entry.grid(column = 2 , row=1)
option1 = tk.OptionMenu(window, school_value_entry_variable, *drop_choice, command = callback1)
school_value_entry_variable.set('0.01')
option1.grid(column = 2 , row=1)
Qiyas_value_entry= tk.Label(window, text="Enter the Value of Qiyas in % (ex. 0.25)")
Qiyas_value_entry.grid(column = 0, row=2)
# Qiyas_value_entry=tk.Entry()
# Qiyas_value_entry.grid(column = 2 , row=2)
option2 = tk.OptionMenu(window, Qiyas_value_entry_variable, *drop_choice, command = callback2)
Qiyas_value_entry_variable.set('0.01')
option2.grid(column = 2 , row=2)
Comprehensive_value_entry= tk.Label(window, text="Enter the Value of Comprehensive in %(ex. 0.30)")
Comprehensive_value_entry.grid(column = 0, row=3)
# Comprehensive_value_entry=tk.Entry()
# Comprehensive_value_entry.grid(column = 2 , row=3)
option3 = tk.OptionMenu(window, Comprehensive_value_entry_variable, *drop_choice, command = callback3)
Comprehensive_value_entry_variable.set('0.01')
option3.grid(column = 2 , row=3)
school= tk.Label(text="High School Grade")
school.grid(column = 0, row=4)
school_entry=tk.Entry(window)
school_entry.grid(column = 2 , row=4)
Qiyas= tk.Label(text="Qiyas Grade")
Qiyas.grid(column = 0, row=5)
Qiyas_entry=tk.Entry(window)
Qiyas_entry.grid(column = 2 , row=5)
Comprehensive= tk.Label(text="Comprehensive Grade ")
Comprehensive.grid(column = 0, row=6)
Comprehensive_entry=tk.Entry(window)
Comprehensive_entry.grid(column = 2 , row=6)
#Calculating the grade& get the value from the user
def calculate_grade():
Mohammed = person('Your Component Grade is:', float(school_entry.get()),
float(Qiyas_entry.get()),
float(Comprehensive_entry.get()),
float(s1),
float(q1),
float(c1))
text_answer= tk.Text(master = window, height= 20, width= 30)
text_answer.grid(column= 2, row= 8)
answer_text= "{} {} ".format(Mohammed.name,"%.2f" % Mohammed.grade())
text_answer.insert(tk.END, answer_text)
claculate_butten= tk.Button(text="Calculate Grade", command= calculate_grade)
claculate_butten.grid(column = 2, row=7)
class person:
def __init__(self, name, a, f, g, x, y, z):
self.name= name
self.a= school_value_entry
self.y= Qiyas_value_entry
self.z= Comprehensive_value_entry
self.x= school
self.f= Qiyas
self.g= Comprehensive
def grade(self):
grade = float(school_entry.get()) * float(s1) + float(Qiyas_entry.get()) * float(q1) + float(Comprehensive_entry.get()) * float(c1)
return grade
window.mainloop()