python按钮事件传入参数_python – 通过Tkinter中的Button将参数传递给...

我目前正在尝试修复python书“Python下一步”中的一个错误,作者尚未修复并在代码中留下评论:“稍后修复”

我的第一个解决方案失败了,但第二个解决方案成功地删除了循环.问题是我无法弄清楚为什么第一个解决方案失败了!

解决方案1:

当用户使用循环点击Tkinter中的计算器中的按钮时,Button对象和一个名为click的函数只是从lambda参数中打印出一个大写字母C.代码由有问题的鳕鱼附近的V形符号评论,在I点我在说什么.

解决方案2:

删除生成按钮的循环并重复手动编码每个按钮!这有效,正如Brian kernighan建议的那样:在使其高效之前先让它工作!

码:

# myCalculator3_final.py

from Tkinter import *

from decimal import *

# key press function:

def click(key):

display.insert(END, key)

##### main:

window = Tk()

window.title("MyCalculator")

# create top_row frame

top_row = Frame(window)

top_row.grid(row=0, column=0, columnspan=2, sticky=N)

# use Entry widget for an editable display

display = Entry(top_row, width=45, bg="light green")

display.grid()

# create num_pad_frame

num_pad = Frame(window)

num_pad.grid(row=1, column=0, sticky=W)

# This method of passing an argument to click work! Loop removed and buttons hand code

#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

#-------------------------------------------------------------------------

# create num_pad buttons passing an argument to the command function click

#-------------------------------------------------------------------------

seven = Button(num_pad, text="7", width=5, command=lambda :click("7"))

seven.grid(row=0,column=0)

eight = Button(num_pad, text="8", width=5, command=lambda :click("8"))

eight.grid(row=0,column=1)

nine= Button(num_pad, text="9", width=5, command=lambda :click("9"))

nine.grid(row=0,column=2)

four= Button(num_pad, text="4", width=5, command=lambda :click("4"))

four.grid(row=1,column=0)

five= Button(num_pad, text="5", width=5, command=lambda :click("5"))

five.grid(row=1,column=1)

six= Button(num_pad, text="6", width=5, command=lambda :click("6"))

six.grid(row=1,column=2)

one= Button(num_pad, text="1", width=5, command=lambda :click("1"))

one.grid(row=2,column=0)

two= Button(num_pad, text="2", width=5, command=lambda :click("2"))

two.grid(row=2,column=1)

three= Button(num_pad, text="3", width=5, command=lambda :click("3"))

three.grid(row=2,column=2)

zero= Button(num_pad, text="0", width=5, command=lambda :click("0"))

zero.grid(row=2,column=0)

#---------------------------------------------------------------------------

# calculate the row, column for button

# create operator_frame

operator = Frame(window)

operator.grid(row=1, column=1, sticky=E)

operator_list = [

'*', '/',

'+', '-',

'(', ')',

'C' ]

# The authors code and I have added the same lambda function as above but

#it just prints out capital C

#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

r = 0

c = 0

for btn_text in operator_list:

Button(operator, text=btn_text, width=5, command=lambda: click(btn_text)).grid(row=r,column=c)

c = c+1

if c > 1:

c = 0

r = r+1

##### Run mainloop

window.mainloop()

题:

为什么单击传递参数的lambda调用方法在循环中不起作用并且只显示C但是如果我删除循环就可以了!

Python,特别是使用Tkinter库创建GUI应用程序时,你可以通过以下几个步骤实现在点击按钮时显示指定路径的图片: 1. 首先,导入所需的库,如Tkinter和PIL(Python Imaging Library用于处理图像): ```python import tkinter as tk from PIL import Image, ImageTk ``` 2. 定义一个函数来打开并预览图片,当button_clicked_1被点击时调用这个函数: ```python def show_image(file_path): # 打开图片 img = Image.open(file_path) # 调整图片大小以适应画布 resized_img = img.resize((500, 500), Image.ANTIALIAS) # 将大小设为500x500像素 # 使用ImageTk将PIL图片转换为Tkinter可以接受的形式 photo = ImageTk.PhotoImage(resized_img) # 创建一个新的Label来显示图片 label = tk.Label(root, image=photo) label.image = photo # 保留引用,防止垃圾回收 label.pack() # 显示图片 ``` 3. 在主程序,你需要创建一个Button实例,并绑定它的command属性到show_image函数传入图片文件路径作为参数: ```python # 创建顶层窗口 root = tk.Tk() # 假设file_path变量存储了图片的实际路径 file_path = "path_to_your_image.jpg" # 创建按钮 button_clicked_1 = tk.Button(root, text="预览图片", command=lambda: show_image(file_path)) button_clicked_1.pack() # 运行主循环 root.mainloop() ``` 确保在运行前替换`"path_to_your_image.jpg"`为实际的图片文件路径。这样,每次点击“预览图片”按钮,都会加载并显示指定路径的图片。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值