如何做到这一点,当在OptionMenu上选择一个Option并按下一个按钮,然后对该特定选项进行操作时

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何做到这一点,当在OptionMenu上选择一个Option并按下一个按钮,然后对该特定选项进行操作时相关的知识,希望对你有一定的参考价值。

我想拥有它,因此在我的选项菜单中按下一个选项并在这种情况下被选中为States。我希望自己的计算功能特别适用于该状态,而不适合其他状态。所有计算功能都有一个公式来计算每个州的税收和工资。选择后请帮助我为这些选项分配值!

import tkinter as tk

#WINDOW PROPERTIES
GUI = tk.Tk()
GUI.title("Payroll Calculator")
#GUI.minsize('305','110')
#GUI.maxsize('305','110')

States = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'District of Columbia', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming']

def saying():
    ("Total weeks pay: ")
hoursWorked = tk.StringVar()
payRate = tk.StringVar()
```

AL2 = tk.StringVar(GUI)
AZ2 = tk.StringVar(GUI)
AK2 = tk.StringVar(GUI)

#BUTTON

#CALCULATED ALABAMA
def calculateAL():
    salaryAL = (ALTax())
    resultsAL = tk.Label(GUI, text = "Total Pay: $" + "%.2f" % salaryAL).grid(row=5, column=1)
def ALTax():
    global hoursWorked, payRate
    return (float(hoursWorked.get()) * float(payRate.get())) - (float(payRate.get()) * AL)
AL2.trace("w", ALTax)
#CALCULATED ARIZONA
def calculateAZ():
    salaryAZ = (AZTax())
    resultsAZ = tk.Label(GUI, text = "Total Pay: $" + "%.2f" % salaryAZ).grid(row=5, column=1)
def AZTax():
    global hoursWorked, payRate
    return (float(hoursWorked.get()) * float(payRate.get())) - (float(payRate.get()) * AZ)
AZ2.trace("w", AZTax)
#CALCULATED ALASKA
def calculateAK():
    salaryAK = (AKTax())
    resultsAK = tk.Label(GUI, text = "Total Pay: $" + "%.2f" % salaryAK).grid(row=5, column=1)
def AKTax():
    global hoursWorked, payRate
    return (float(hoursWorked.get()) * float(payRate.get())) - (float(payRate.get()) * AK)
AK2.trace("w", AZTax)

#TAXES
AL = 0.035
AZ = 0.03565
AK = 1
AR = 0.039
CA = 0.0715
CO = 1
CT = 0.04995
DE = 0.044
FL = 1
GA = 0.035
HI = 0.04825
ID = 4.5
IL = 1
IN = 1
IA = 0.0467
KS = 0.0405
KY = 0.04
LA = 0.04
ME = 0.06475
MD = 0.03875
MA = 1
MI = 1
MN = 0.076
MS = 0.04
MO = 0.0375
MT = 0.0395
NE = 0.0465
NV = 1
NH = 1
NJ = 0.05185
NM = 0.033
NY = 0.0641
NC = 1
ND = 0.02
OH = 0.0275
OK = 0.0275
OR = 0.0745
PA = 1
RI = 0.0487
SC = 0.035
SD = 1
TN = 1
TX = 1
UT = 1
VT = 0.0625
VA = 0.03875
WA = 1
WV = 0.0475
WI = 0.05825
WY = 1
DC = 0.06475

#ENTRY
tk.Label(GUI, text='Pay Rate:', font="110").grid(row=0)
tk.Label(GUI, text='Hours Worked:', font="110").grid(row=1)

e1 = tk.Entry(GUI, textvariable = payRate)
e2 = tk.Entry(GUI, textvariable = hoursWorked)
e1.grid(row = 0, column = 1, sticky = tk.W)
e2.grid(row = 1, column = 1, sticky = tk.W)

#OPTION MENU
opt = tk.StringVar()
opt.set("Choose a state")

option = tk.OptionMenu(GUI, opt, *States).grid(row=2, column=0)

def show():
    print(option.get()) 

frame = tk.Frame(GUI)
frame.grid()

button = tk.Button(frame, text='Calculate', fg='red', command=calculateAL, calculateAK, calculateAZ)
button.grid(row=1, column=0)
```

#SET DEFAULTS
hoursWorked.set(0)
payRate.set(0)

#EVENT LOOP
GUI.mainloop()
答案
import tkinter as tk
#init root
root1 = tk.Tk()
#set location
fields = ['some input variable','some option variable']
for field in fields:
    row = tk.Frame(root1)
    lab = tk.Label(row, width=33,text=field+': ',anchor='w')
    if field == 'some input ':
        ent = tk.Entry(row)
        ent.insert(0,'ANY DEFAULT VALUE')
        ent.pack(side=tk.RIGHT,expand=tk.YES,fill=tk.X)
        row.pack(side=tk.TOP,fill=tk.X)
        lab.pack(side=tk.LEFT)
        continue
    elif field == 'some option variable':
        #spec options
        options = ['ALL'] + ['a'] + .....
        variable = tk.StringVar(root1)
        ent = tk.OptionMenu(row,variable,*options)
        #unpack the setting.
        row.pack(side=tk.TOP,fill=tk.X)
        lab.pack(side=tk.LEFT)
        ent.pack(side=tk.RIGHT,expand=tk.YES,fill=tk.X)
root1.mainloop()

以上是关于如何做到这一点,当在OptionMenu上选择一个Option并按下一个按钮,然后对该特定选项进行操作时的主要内容,如果未能解决你的问题,请参考以下文章

在 SQL PARTITION 上选择最新项目

以编程方式在谷歌地图颤动上选择一个标记

在单击反应表上选择行

无法在 Instruments 上选择跟踪活动分配

在其中一个联接上选择具有最大值的记录

使用 T-SQL 中的 OVER 子句在除一列之外的所有列上选择 DISTINCT