简体   繁体   English

通过使用Tkinter:将Entry转换为下拉菜单以进行选择:

[英]By using Tkinter : Convert the Entry to drop down menu to choose from:

I am using Tkinter and would like to convert the first three tk.Entry() from Entry to drop down menu that enables the user to choose the percent (0.01%, 0.02%, ...etc) 我正在使用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()

Well looking at your code there are several issues. 看代码,有几个问题。 The class person seams pointless as everything there can be done in a function. person接缝毫无意义,因为在函数中可以完成所有操作。 You assign variables but then you use the .get() method again instead of using the variables your created. 您分配了变量,但是然后再次使用.get()方法,而不是使用创建的变量。 So some stuff in your code is pointless. 因此,代码中的某些内容毫无意义。

That being said I have added some drop down menu boxes in the places of the first 3 entry boxes. 话虽如此,我在前三个输入框的位置添加了一些下拉菜单框。 The way I set it up may not be perfect but it is a start and you should be able improve on the code to meet you needs. 我设置它的方式可能并不完美,但这只是一个开始,您应该可以对代码进行改进以满足您的需求。

First I created a dictionary that we will use with the drop down boxes to provide the values of each percentage and I created some variables that we will be using later. 首先,我创建了一个字典,将其与下拉框一起使用以提供每个百分比的值,并创建了一些稍后将使用的变量。

drop_choice = {}
s1 = "0.01"
q1 = "0.01"
c1 = "0.01"

Second I created a few functions that will be used to update s1, q1 and c1 each time their respective drop downs change. 其次,我创建了一些函数,每当它们各自的下拉列表更改时,这些函数将用于更新s1,q1和c1。

def callback1(value):
    global s1
    s1 = value

def callback2(value):
    global q1
    q1 = value

def callback3(value):
    global c1
    c1 = value

Third I created a function that adds all the values to our drop_choice dictionary. 第三,我创建了一个函数,将所有值添加到我们的drop_choice字典中。

I did not know exactly all the values you wanted so I created 0.01-1.00. 我不完全知道您想要的所有值,所以我创建了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()

Forth I created the StringVar() 's for each of the drop down menus. 为此,我为每个下拉菜单创建了StringVar()

school_value_entry_variable = tk.StringVar()
Qiyas_value_entry_variable = tk.StringVar()
Comprehensive_value_entry_variable = tk.StringVar()

Last major change is creating the drop down menus. 最后的主要更改是创建下拉菜单。

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)

After that I changed up some of the formulas to use the s1, q1 and c1 variables I first created. 之后,我修改了一些公式以使用我首先创建的s1,q1和c1变量。

With all this info you should be able to get a better understanding of how to set up these drop down menus. 通过所有这些信息,您应该能够更好地了解如何设置这些下拉菜单。

Below is a copy paste version of your code with the added drop downs: 以下是代码的复制粘贴版本以及添加的下拉列表:

I left the code in where you had your first 3 entry widgets. 我将代码留在了您的前三个输入小部件中。 I just commented them out and added the drop down menus. 我只是将它们注释掉并添加了下拉菜单。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM