"button_back = Button(root, text = " << ", command = lambda: back(image_number-1)) TypeError:



我想用Tkinter做一个小应用程序,它是关于照片查看器的

我的界面上有3个按钮[后退、退出、前进],退出按钮和前进按钮工作良好,但不幸的是,我在后退按钮中遇到了一些错误

前进按钮:前进到下一张照片退出按钮:退出应用程序后退按钮:返回上一张照片

错误为:

"button_back=按钮(root,text=<<",command=lambda:back(image_number-1((TypeError:back((接受0个位置参数但给予1";

代码:

from Tkinter import *
from PIL import ImageTk, Image
my_img1 = ImageTk.PhotoImage(Image.open("one.png")) #-First Step
my_img2 = ImageTk.PhotoImage(Image.open("two.png")) 
my_img3 = ImageTk.PhotoImage(Image.open("three.png")) 
my_img4 = ImageTk.PhotoImage(Image.open("four.png")) 
image_list = [my_img1, my_img2, my_img3, my_img4]
my_label = Label(image=my_img1)     #Three Button
my_label.grid(row =0, column = 0, columnspan=3)
def back():
global my_label
global button_forward
global button_back
def forward(image_number):
global my_label
global button_forward
global button_back

my_label.grid_forget()
my_label = Label(image=image_list[image_number-1])

button_forward = Button(root, text = ">>", command = 
lambda:forward(image_number+1))
#ERROR
button_back = Button(root, text = "<<", command = lambda: back(image_number-1))

my_label.grid(row =0, column = 0, columnspan=3)
button_back.grid(row= 1, column=0)
button_forward.grid(row = 1, column = 2)



button_back = Button(root, text = "<<", command = back)
button_forward = Button(root, text = ">>", command = lambda : forward(2))
button_exit = Button(root, text = "EXIT PROGRAM", command = root.quit)

button_back.grid(row= 1, column=0)
button_exit.grid(row = 1, column = 1)
button_forward.grid(row = 1, column = 2)
root.mainloop()

您在def back(image_number):中忘记了image_number
并且在启动时还需要command=lambda:back(0)


但坦率地说,使用全局变量current_image并直接在函数中使用该变量可能更简单。

它不需要再次创建按钮(或为button_forward["command"] = lambda:forward(image_number+1)等按钮分配新功能(

您不必再次创建标签,但可以替换现有标签中的图像-my_label["image"] = ...


可能是这样的:

import tkinter as tk  # PEP8: `import *` is not preferred
from PIL import ImageTk  # no need `Image` if you don't change images - ie. resize, crop, convert to grayscale
#import glob
# --- functions ---  # PEP8: all functions before main code
def back():
global current_image
if current_image > 0:
current_image -= 1
#else:    # looping images
#    current_image = len(image_list)-1
my_label["image"] = image_list[current_image]
def forward():
global current_image

if current_image < len(image_list)-1:
current_image += 1
#else:    # looping images
#    current_image = 0

my_label["image"] = image_list[current_image]
# --- main ---
filenames = ["one.png", "two.png", "three.png", "four.png"]
#filenames = glob.glob("*.png") + glob.glob("*.jpg")
# ---
root = tk.Tk()
image_list = []
for name in filenames:
img = ImageTk.PhotoImage(file=name)  # without `Image()` it has to use `file=`
image_list.append(img)
current_image = 0
# ---
my_label = tk.Label(image=image_list[current_image])
my_label.grid(row=0, column=0, columnspan=3)
button_back = tk.Button(root, text="<<", command=back)  # PEP8: inside `()` without spaces aroung `=`
button_forward = tk.Button(root, text=">>", command=forward)
button_exit = tk.Button(root, text="EXIT PROGRAM", command=root.destroy)  # `.destroy` is better then `.quit`
button_back.grid(row=1, column=0)
button_exit.grid(row=1, column=1)
button_forward.grid(row=1, column=2)
root.mainloop()

PEP 8——Python代码的样式指南

相关内容

最新更新