You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import tkinter as tk
from PIL import Image, ImageTk
from alien_invasion import AlienInvasion
class ImageSwitcher:
def __init__(self, master, images_paths):
self.master = master
self.master.title("选择你想要的飞机")
self.images = []
self.image_frames = []
self.current_image_index = 0
rows = (len(images_paths) - 1) // 3 + 1
column = 0
row = 0
for i, path in enumerate(images_paths):
frame = tk.Frame(master)
img = Image.open(path)
img = img.resize((400, 260), Image.BICUBIC)
img_tk = ImageTk.PhotoImage(img)
self.images.append(img_tk)
image_label = tk.Label(frame, image=img_tk)
image_label.grid(row=0, column=0, padx=5, pady=5) # 在frame内部使用grid
button = tk.Button(frame, text=f"选择飞机 {i+1}", command=lambda index=i: self.switch_image(index),
font=("Arial", 18), padx=10, pady=5)
button.grid(row=0, column=1, padx=5, pady=5) # 在frame内部与图片并排
# 将frame添加到master中但注意使用grid而不是pack
frame.grid(row=row, column=column, padx=10, pady=10)
# 更新行列索引以进行下一组图片和按钮的放置
column += 2 # 因为每个frame包含1个图片和1个按钮所以column增加2
if column >= 3: # 假设每行3个组合
column = 0
row += 1
# 添加"确定"按钮到master的底部
button_show = tk.Button(master, text="确定", command=self.on_button_click,
font=("Arial", 18), padx=10, pady=5)
button_show.grid(row=rows, column=0, columnspan=3, pady=10) # 使其横跨3列
def show_current_image(self):
for frame in self.image_frames:
frame.pack_forget()
current_frame = self.image_frames[self.current_image_index]
current_frame.pack(pady=10)
def switch_image(self, index):
if 0 <= index < len(self.images):
self.current_image_index = index
self.show_current_image()
def on_button_click(self):
selected_image_path = images_paths[self.current_image_index]
alien_invasion = AlienInvasion(selected_image_path)
alien_invasion.run_game(selected_image_path)
self.master.destroy()
print(f"用户选择了图片: {selected_image_path}")
images_paths = ["images\火箭1.png", "images\火箭2.png", "images\火箭3.png","images\火箭4.png"]
def category():
root = tk.Tk()
root.geometry("1200x700")
root.configure(background='#666666') # 白色背景
app = ImageSwitcher(root, images_paths)
root.mainloop()