我正在做一个研究项目,我需要在实验中生成一些图像。但是,我似乎无法使用python保存我在Tkinter中创建的图像。我正在以不同的模式生成一些图像,但我找不到任何有关如何保存它的解决方案。
如果有人知道我该怎么做,那会很棒,并希望你度过愉快的一天! :)from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog
import random
root = Tk()
root.configure(background='white')
green = PhotoImage(file="C:\Users\oscar\Desktop\Patterns\green.pbm")
red = PhotoImage(file="C:\Users\oscar\Desktop\Patterns\\red.pbm")
r = 0
c = 0
while r <= 16:
for c in range(17):
label = Label(root, image=random.choice([green, red]))
label.grid(row=r, column=c)
label.bd = 0
label.configure(border=0)
r += 1
root.mainloop()
# Here is the closest solution I have for saving, but it doesn't work
root.filename = tkFileDialog.asksaveasfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))