【问题标题】:Python tkinter: replace a random image from a list with another one by clicking buttonPython tkinter:通过单击按钮将列表中的随机图像替换为另一个图像
【发布时间】:2020-12-28 13:13:00
【问题描述】:

我的目的是通过单击按钮在列表中显示随机图像餐。我已经成功了第一步,但是当我再次单击该按钮时,我想要替换另一个生成的先前图像。目前,所有生成的图像都是重叠的(参见example)。我已经尝试了很多东西,比如 canvas.delete("all")、.destroy() 和其他类似的东西,但在我的情况下没有任何效果......

你能帮我吗?这是我的代码:

import os, random
from tkinter import *


# WINDOW CREATION
window = Tk()
# WINDOW's setup
window.title("Choosing meal")
window.geometry("500x500")
window.config(background = 'blue')

# FIRST BOX QUESTION: 
frame = Frame(window,bg = 'blue')

question_tilte = Label(frame, text = 'Which meal are you going to eat today ?', font = ("Arial",20), bg = 'blue', fg = "white")
question_tilte.pack()
# ADD THE BOX IN THE WINDOW 
frame.pack(expand = YES)

# SECOND BOX CREATION TO PUT THE IMAGE IN:
frame2 = Frame(window, bg = 'blue')

# RANDOM MEAL CHOOSE METHOD:
def choosing_meal():
    if os.path.exists("meals.txt"):
      with open("meals.txt","r") as file:
            meals_list = file.readlines()
            meal_random_choice = random.choice(meals_list)
            print(meal_random_choice)
            file.close()
            # LISTS 
            canvas = [canvas1,canvas2,canvas3,canvas4]
            for i in range(0,len(meals_list)):
                if meal_random_choice == meals_list[i]:
                  canvas[i].grid(row=2,column=1)
    


# MEAL BUTTON GENERATOR :
meal_generator_button = Button(frame, text = "Choose a random meal !", font = ("Helvetica",15), bg = 'white', fg = 'blue', command = choosing_meal)
meal_generator_button.pack(pady = 25, fill = X) # gère les dimensions du bouton

# Image pizza
width = 332
height = 234
image1 = PhotoImage(file = "pizza_fresca.png")
canvas1 = Canvas(frame2,width = width, height = height, bg = "blue", bd = 0,highlightthickness=0)
canvas1.create_image(width/2,height/2, image = image1)

# Image sushis
width2 = 292
height2 = 164
image2 = PhotoImage(file = "sushi.png")
canvas2 = Canvas(frame2,width = width2, height = height2, bg = "blue", bd = 0,highlightthickness=0)
canvas2.create_image(width2/2,height2/2, image = image2) 

# Image crêpes
width3 = 273
height3 = 185
image3 = PhotoImage(file = "crepes.png")
canvas3 = Canvas(frame2,width = width3, height = height3, bg = "blue", bd = 0,highlightthickness=0)
canvas3.create_image(width3/2,height3/2, image = image3)
              
# Image tacos
width4 = 279
height4 = 125
image4 = PhotoImage(file = "tacos.png")
canvas4 = Canvas(frame2,width = width4, height = height4, bg = "blue", bd = 0,highlightthickness=0)
canvas4.create_image(width4/2, height4/2, image = image4)
 
frame2.pack(expand=YES)

# DISPLAY INTERFACE:
window.mainloop()

【问题讨论】:

    标签: python image tkinter random replace


    【解决方案1】:

    您应该在所选画布以外的画布上调用grid_forget()

    for i in range(0,len(meals_list)):
        if meal_random_choice == meals_list[i]:
            canvas[i].grid(row=2,column=1)
        else:
            canvas[i].grid_forget()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多