请问各位大神如何将下拉菜单的文字设定成一个值?
python吧
全部回复
仅看楼主
level 1
from tkinter import *
from tkinter.ttk import Combobox
class Calculator:
def __init__(self):
# 窗口和标题
window = Tk()
window.title("价格计算器")
# 定义StringVar对象动态存储输入框的值
self.fruitQTYVar = StringVar()
self.fruittypeVar = ['苹果','梨子','香蕉']
self.totaVar = StringVar()
# 设置一些默认值
self.total.set("0.00")
# 整体面板,方便设置总体的边距
frame = Frame(window)
frame.pack(padx=10, pady=10)
# 将标签排列在面板的第一列
Label(frame, text="水果种类",font = ('黑体',20)).grid(row=1, column=1,sticky=W)
Label(frame, text="水果单价",font = ('黑体',20)).grid(row=2, column=1, pady=5,sticky=W)
Label(frame, text="水果总价",font = ('黑体',20)).grid(row=3, column=1, pady=5,sticky=W)
# textvariable = self.amountVar 由相应的StringVar对象动态保存输入框中的文本
Entry(frame, justify=RIGHT, textvariable=self.fruitQTYVar,font = ('黑体',20)).grid(row=1, column=2)
Combobox(frame, justify=RIGHT,values=self.fruittypeVar,font = ('黑体',20)).grid(row=2, column=2)
Label(frame, text="0.00",textvariable=self.totaVar,font = ('黑体',20)).grid(row=3, column=2, sticky=E)
# 面板第三列,一堆Message存储单位
Message(frame, text="米",font = ('黑体',20)).grid(row=1, column=3)
Message(frame, text="米",font = ('黑体',20)).grid(row=2, column=3)
Message(frame, text="米",font = ('黑体',20)).grid(row=3, column=3)
# 空Frame以撑开空间
Frame(frame, height=10).grid(row=19, column=1, columnspan=3, pady=5)
# 按钮,事件监听函数为calculate
Button(frame, width=40, text="计算", command=self.calculate,font = ('黑体',20)).grid(row=20, column=1, columnspan=3, pady=0)
# 按钮点击监听
def calculate(self):
# 获取输入的参数
fruitQTY = eval(self.fruitQTYVar.get())
# 获取名称填入对应价格
fruitprice = fruittype
if fruittype = 苹果
fruitprice = 5
if fruittype = 梨子
fruitprice = 6
if fruittype = 香蕉
fruitprice = 8
# 计算价格
totalprice = fruitQTY * fruitprice
# 将计算结果设置给控件
self.totalVar.set(format(totalprice,".2f"))
2018年09月25日 02点09分 1
level 1
现在一直告诉我语法错误,下面这个部分应该怎么改···
# 获取名称填入对应价格
fruitprice = fruittype
if fruittype = 苹果
fruitprice = 5
if fruittype = 梨子
fruitprice = 6
if fruittype = 香蕉
fruitprice = 8
2018年09月25日 02点09分 2
判断相等 ==
2018年09月26日 01点09分
level 6
现在一直告诉我语法错误,下面这个部分应该怎么改···
# 获取名称填入对应价格
fruitprice = fruittype 前者不是后者的 引用吗? 这不能一会字符串一会数值吧
2018年09月25日 15点09分 4
那要字典是吗?大神我应该怎么改?
2018年09月25日 16点09分
@巴蜀烂柯人 字典可以 键值对存放,判断键 然后打印取出的值
2018年09月25日 16点09分
@luu92 字典以后,只能print是吗,不能用get是吗?
2018年09月26日 05点09分
@巴蜀烂柯人 get是 取值 print只是把取得值 进行打印 没什么关系和影响
2018年09月26日 06点09分
1