python-3.x - tkinter - 单击按钮时显示数组中的随机图像
问题描述
我想要一个标签在单击按钮时显示随机图像。
这是我的方法,但它不起作用。欢迎任何关于如何解决的想法。
from tkinter import *
import random
window = Tk()
filechoices = ["image1.png", "image2.png", "image3.png"]
filename = PhotoImage(file = random.choice[filechoices])
def press():
image = Label(window, image=filename).pack()
button1 = Button(window, text="click to see image", command = press)
button1.pack()
解决方案
random.choice
是一个函数,而不是一个列表。
它应该是:
filename = PhotoImage(file = random.choice(filechoices))
读取随机模块
random.choice(seq) 从非空序列 seq 返回一个随机元素。
另外,在您的代码中,您还没有使用mainloop()
推荐阅读
- android - FaunaDB 驱动程序能否以某种方式在 Android 上运行?
- reactjs - 样式化的组件和优化
- visual-studio - 从 Azure.Identity 访问用户凭据值
- javascript - 事件范围的设置值无法识别自动在范围内输入字符串的自定义函数
- python - 如何在 django 中自定义 404 页面?
- android - 自动化 NDK 测试,多种 API
- r - 在 R 中使用 gdal_translate 重新采样 Arc/Info 二进制网格文件
- scala - 合并后如何保留两个可观察对象发出的项目的顺序?
- postgresql - Postgres - 创建或替换表
- pentaho - 从文件名每月更新的目录中输入数据