从tkinter窗口获取值,对其进行划分,然后在列表中对它们进行排序

时间:2019-05-20 04:48:15

标签: python tkinter

我正在尝试制作一个python程序,该程序将输入到tkinter输入字段中的数字除以它们,然后将它们分类到列表中,然后创建一个显示哪个是最低的消息框。

取值的字段图像

然后,应将它们划分并以列表/字典的形式排列所有条目的商,并打印出最低的。

例如。项目#1最便宜,项目#3最便宜,等等。

到目前为止,这是我的代码:

from tkinter import * #Import the Tkinter module
from tkinter import messagebox #Imports the message box module
from tkinter import font #Imports fonts from tkinter
import json #Imports JSON

a = ('') #Variable for price and quantity/mass
b = ('') #^
c = ('') #^
d = ('') #^
e = ('') #^
f = ('') #^
r = ('') #Result Var
aa = ('')
ba = ('')
ca = ('')
da = ('')
q1 = ('')
q2 = ('')
q3 = ('')
q4 = ('')
c = 1
cp = 1
ca = 1

d = 1
dp = 1
da = 1

count = 2 #For extra Fields

global row_count, entries, item_count, output
row_count = 10
item_count = 2
entries = []
output = []


master = Tk() #Tkinter

m = StringVar(master) #Defines m as yhe variable from the option menu
c = StringVar(master) #Defines m as yhe variable from the option menu

entry1 = Entry(master) #Entry Feilds
entry2 = Entry(master)

entry3 = Entry(master) #Entry Feilds (Variables)
entry4 = Entry(master)

entryq1 = Entry(master) #Entry Feilds (Variables)
entryq2 = Entry(master)


def create_entry():
    global row_count, entries, item_count
    item_count += 1

    Label(master, text=("Item #"+ str(item_count)), font=("Helvetica", 20, "bold underline")).grid(row=(row_count))

    temp_entry = Entry(master)
    temp_entry.grid(row=(row_count+1), column=1)

    entries.append(temp_entry)
    Label(master, textvariable=c).grid(row=(row_count+1))

    temp_entry = Entry(master)
    temp_entry.grid(row=(row_count+2), column=1)

    entries.append(temp_entry)
    Label(master, textvariable=m).grid(row=(row_count+2))

    temp_entry = Entry(master)
    temp_entry.grid(row=(row_count+2), column=1)

    entries.append(temp_entry)
    Label(master, text='Rating out of 5').grid(row=(row_count+3))

    temp_entry = Entry(master)
    temp_entry.grid(row=(row_count+3), column=1)


    row_count += 4

def showinfo():
   messagebox.showinfo("Best buy", r) #Creates a message box with pops up which shows r (result)


def addnew():
   global count, entryq1, entryq2
   count = count+1
   create_entry()

def sort_entries():
    for entry in entries:   
        pass



def calculate():
   print(count) #Debugging
   if count == 2:
      item_two()
   else:
       sort_entries()

def item_two():
   q1 = float(entryq1.get())
   q2 = float(entryq2.get())
   checkrating(q1, '(First rating)')
   checkrating(q2, '(Second rating)')
   a = entry1.get() #Gets value from the entry fields
   ap = entry2.get() 
   b = entry3.get()
   bp = entry4.get()
   aa = float(a)/float(ap) #Calcultes Price per item/ quantity of item 1
   ba = float(b)/float(bp) #Calculates price per item/quantity of item 2

   if aa == ba: #Finds which one is better
       print('They are the same pice') #Outputs value in output
       quailitys()
   else:
       if aa > ba:
           print('The second item is cheaper') #^
           r = ('The second item is cheaper') #Sets Value to R
       else:
           if ba > aa:
              print('The first item is cheaper') #^
              r = ('The first item is cheaper') #Sets Value to R
           else:
              r = ('Malfunction Occured Please check the input Values')
       messagebox.showinfo("Best buy", r) #Creates a message box with pops up which shows r (result)   



def checkrating(var, rantingno):
    try:
        var = float(var)
        if var > 5 or var < 0:
           messagebox.showinfo("Best buy", "Please Enter a Valid Number for the rating", '(', rantingno, ')')
    except ValueError:
        messagebox.showinfo("Best buy", "Please Enter a Valid Number for the rating")

def quailitys():
   if count == 2:
      q1 = float(entryq1.get())
      q2 = float(entryq2.get())
      if q1 == q2: #Finds which one is better
       print('They are the same value') #Outputs value in output
       r = ('They are the same value') #Sets Value to R
      elif q1 < q2:
           print('The second item is better value') #^
           r = ('The second item is better value') #Sets Value to R
      elif q2 < q1:
              print('The first item is better value') #^
              r = ('The first item is better value') #Sets Value to R
      messagebox.showinfo("Best buy", r) #Creates a message box with pops up which shows r (result)



Label(master, text="Best Buy Calculator", font=("Helvetica", 20, "bold underline")).grid(row=0) 
Label(master, text="Item #1", font=("Helvetica", 17, "bold underline")).grid(row=2) #1st Item Fields
Label(master, textvariable=c).grid(row=3)
Label(master, textvariable=m).grid(row=4)


entry1.grid(row=3, column=1) #Puts Entry fields in a certain location
entry2.grid(row=4, column=1) # ^


entryq1.grid(row=5, column=1) # ^
entryq2.grid(row=9, column=1) # ^
Label(master, text='Rating out of 5').grid(row=5)
Label(master, text='Rating out of 5').grid(row=9)


Label(master, text="Item #2", font=("Helvetica", 17, "bold underline")).grid(row=6) #2nd Item Fields
Label(master, textvariable=c).grid(row=7)
Label(master, textvariable=m).grid(row=8)


entry3.grid(row=7, column=1) #2nd Item Entry fields
entry4.grid(row=8, column=1)


Button(master, text='+', command=addnew).grid(row=100, column=0, pady=4) #Cancel Button
Button(master, text='Cancel', command=quit).grid(row=101, column=0, pady=4) #Cancel Button
Button(master, text='Find Better Value', command=calculate).grid(row=101, column=1, pady=4) #Button to calculate better value



m.set("Measurement") # This is what the drop down box shows when no options are selected
c.set('Currency')
mes = OptionMenu(master, m, "Weight (kg)","Weight (g)", "Volume (mL)", "Quantity").grid(row=1) #Options and grid placement of the option menu
cur = OptionMenu(master, c, "AUD ($)","USD ($)", "EUR (€)", "GBP (£)", "CNY (¥)").grid(row=1, column=1) #Options and grid placement of the option menu



mainloop()

任何想法都将不胜感激。谢谢! :)

修改后的代码

from tkinter import * #Import the Tkinter module
from tkinter import messagebox #Imports the message box module
from tkinter import font #Imports fonts from tkinter
import json #Imports JSON

a = ('') #Variable for price and quantity/mass
b = ('') #^

r = ('') #Result Var

aa = ('')
ba = ('')

q1 = ('')
q2 = ('')

count = 2 #For extra Fields

global row_count, entries, item_count, output
row_count = 10
item_count = 2
entries = []
output = []
price = {}
sort_count = 1

master = Tk() #Tkinter

m = StringVar(master) #Defines m as yhe variable from the option menu
c = StringVar(master) #Defines m as yhe variable from the option menu

entry1 = Entry(master) #Entry Feilds
entry2 = Entry(master)

entry3 = Entry(master) #Entry Feilds (Variables)
entry4 = Entry(master)

entryq1 = Entry(master) #Entry Feilds (Variables)
entryq2 = Entry(master)


def bestCur(*entries):
    # each entry should be a tuple e.g. (curEntry, mesEntry)
    entries = list(entries)
    for entry in entries:
        entry.append(float(entry[0].get())/float(entry[1].get()))
    entries.sort(key=lambda x: x[2])
    print(entries)


but1 = Button(master, text="press me", bg="red", command=lambda: bestCur([entry1, entry2], [entry3, entry4]))
but1.grid(row=101, column=2)

def create_entry():
    global row_count, entries, item_count
    item_count += 1

    Label(master, text=("Item #"+ str(item_count)), font=("Helvetica", 20, "bold underline")).grid(row=(row_count))

    temp_entry = Entry(master)
    temp_entry.grid(row=(row_count+1), column=1)

    entries.append(temp_entry)
    Label(master, textvariable=c).grid(row=(row_count+1))

    temp_entry = Entry(master)
    temp_entry.grid(row=(row_count+2), column=1)

    entries.append(temp_entry)
    Label(master, textvariable=m).grid(row=(row_count+2))

    temp_entry = Entry(master)
    temp_entry.grid(row=(row_count+2), column=1)

    entries.append(temp_entry)
    Label(master, text='Rating out of 5').grid(row=(row_count+3))

    temp_entry = Entry(master)
    temp_entry.grid(row=(row_count+3), column=1)


    row_count += 4

def showinfo():
   messagebox.showinfo("Best buy", r) #Creates a message box with pops up which shows r (result)

def addnew():
   global count, entryq1, entryq2
   count = count+1
   create_entry()

def sort_entries():
    while sort_count != count:
        if sort_count == 2:
            a = entry1.get() #Gets value from the entry fields
            ap = entry2.get() 
            b = entry3.get()
            bp = entry4.get()
            aa = float(a)/float(ap) #Calcultes Price per item/ quantity of item 1
            ba = float(b)/float(bp) #Calculates price per item/quantity of item 2
    pass

def calculate():
   print(count) #Debugging
   if count == 2:
      item_two()
   else:
       sort_entries()

def item_two():
   q1 = float(entryq1.get())
   q2 = float(entryq2.get())
   checkrating(q1, '(First rating)')
   checkrating(q2, '(Second rating)')
   a = entry1.get() #Gets value from the entry fields
   ap = entry2.get() 
   b = entry3.get()
   bp = entry4.get()
   aa = float(a)/float(ap) #Calcultes Price per item/ quantity of item 1
   ba = float(b)/float(bp) #Calculates price per item/quantity of item 2

   if aa == ba: #Finds which one is better
       print('They are the same pice') #Outputs value in output
       quailitys()
   else:
       if aa > ba:
           print('The second item is cheaper') #^
           r = ('The second item is cheaper') #Sets Value to R
       else:
           if ba > aa:
              print('The first item is cheaper') #^
              r = ('The first item is cheaper') #Sets Value to R
           else:
              r = ('Malfunction Occured Please check the input Values')
       messagebox.showinfo("Best buy", r) #Creates a message box with pops up which shows r (result)   

def checkrating(var, rantingno):
    try:
        var = float(var)
        if var > 5 or var < 0:
           messagebox.showinfo("Best buy", "Please Enter a Valid Number for the rating", '(', rantingno, ')')
    except ValueError:
        messagebox.showinfo("Best buy", "Please Enter a Valid Number for the rating")

def quailitys():
   if count == 2:
      q1 = float(entryq1.get())
      q2 = float(entryq2.get())
      if q1 == q2: #Finds which one is better
       print('They are the same value') #Outputs value in output
       r = ('They are the same value') #Sets Value to R
      elif q1 < q2:
           print('The second item is better value') #^
           r = ('The second item is better value') #Sets Value to R
      elif q2 < q1:
              print('The first item is better value') #^
              r = ('The first item is better value') #Sets Value to R
      messagebox.showinfo("Best buy", r) #Creates a message box with pops up which shows r (result)

Label(master, text="Best Buy Calculator", font=("Helvetica", 20, "bold underline")).grid(row=0) 
Label(master, text="Item #1", font=("Helvetica", 17, "bold underline")).grid(row=2) #1st Item Fields
Label(master, textvariable=c).grid(row=3)
Label(master, textvariable=m).grid(row=4)


entry1.grid(row=3, column=1) #Puts Entry fields in a certain location
entry2.grid(row=4, column=1) # ^


entryq1.grid(row=5, column=1) # ^
entryq2.grid(row=9, column=1) # ^
Label(master, text='Rating out of 5').grid(row=5)
Label(master, text='Rating out of 5').grid(row=9)


Label(master, text="Item #2", font=("Helvetica", 17, "bold underline")).grid(row=6) #2nd Item Fields
Label(master, textvariable=c).grid(row=7)
Label(master, textvariable=m).grid(row=8)


entry3.grid(row=7, column=1) #2nd Item Entry fields
entry4.grid(row=8, column=1)


Button(master, text='+', command=addnew).grid(row=100, column=0, pady=4) #Cancel Button
Button(master, text='Cancel', command=quit).grid(row=101, column=0, pady=4) #Cancel Button
Button(master, text='Find Better Value', command=calculate).grid(row=101, column=1, pady=4) #Button to calculate better value


m.set("Measurement") # This is what the drop down box shows when no options are selected
c.set('Currency')
mes = OptionMenu(master, m, "Weight (kg)","Weight (g)", "Volume (mL)", "Quantity").grid(row=1) #Options and grid placement of the option menu
cur = OptionMenu(master, c, "AUD ($)","USD ($)", "EUR (€)", "GBP (£)", "CNY (¥)").grid(row=1, column=1) #Options and grid placement of the option menu


mainloop()

1 个答案:

答案 0 :(得分:0)

这是可以这样解决的问题:

entry1 = Entry()
entry2 = Entry()
entry3 = Entry()
entry4 = Entry()

def bestCur(*entries):
    # each entry should be a tuple e.g. (curEntry, mesEntry)
    entries = list(entries)
    for entry in entries:
        entry.append(float(entry[0].get())/float(entry[1].get()))
    entries.sort(key=lambda x: x[2])
    print(entries)


but1 = Button(master, text="press me", bg="red", command=lambda: bestCur([entry1, entry2], [entry3, entry4]))
but1.grid(row=10, column=1)

条目是tuple个条目组

例如在示例代码中,当entriesbut1

时,我给出了entries = ([entry1, entry2], [entry3, entry4])

.bestCur(*entries)函数是对条目进行划分和排序的函数。