简体   繁体   English

将图像添加到python中的星形图案打印

[英]Add image to star pattern printing in python

How do I print this on GUI(Tkinter) so that I can add image to its background or is there any other way to add image to this program?如何在 GUI(Tkinter) 上打印它以便我可以将图像添加到其背景中,或者有没有其他方法可以将图像添加到该程序? I have tried importing it on Tkinter but didn't know how to print this there我试过在 Tkinter 上导入它,但不知道如何在那里打印

# List for the loop print star message
print_H=[[" " for i in range(6)]for j in range(6)]
print_A=[[" " for i in range(6)]for j in range(6)]
print_P=[[" " for i in range(6)]for j in range(6)]
print_Y=[[" " for i in range(6)]for j in range(6)]

# Loop for H "*" pattern 
for r in range(6):
    for c in range(6):
        if c == 0 or c == 5 or (r ==  3 and (c>0 and c <5)):
            print_H[r][c]="*"

# Loop for A "*" pattern
for r in range(6):
    for c in range(6):
        if ((c == 0 or c==5) and  r!=0)  or (r==0 and(c>0 and c<5)) or (r==3  and(c>0 and c<5)) :
            print_A[r][c]="*"

# Loop for P"*" pattern
for r in range(6):
    for c in range(6):
        if c==0 or (c==4 and (r==1 or r==2)) or ((r==0 or r==3) and (c>0 and c<4)) :
            print_P[r][c]="*"

# Loop for Y "*" pattern
for r in range(6):
    for c in range(6):
        if (c==2 and r>1) or (r==c and c<2) or (r==0 and c==4) or (r==1 and c==3) :
            print_Y[r][c]="*"

# This will print all star("*") in the message form       
for i in range(6):
    for j in range(6):
        print(print_H[i][j],end=" ")
    for j in range(6):
        print(print_A[i][j],end=" ")
    for j in range(6):
        print(print_P[i][j],end=" ")
    for j in range(6):
        print(print_P[i][j],end=" ")
    for j in range(6):
        print(print_Y[i][j],end=" ")
    print( )

So, if you want to put keyboard symbols in tkinter, you have a few options depending on what you want to do with them.所以,如果你想把键盘符号放在 tkinter 中,你有几个选项,这取决于你想用它们做什么。

To get started, you need to add tkinter to your program and set up a window:首先,您需要将 tkinter 添加到您的程序并设置一个窗口:

import tkinter as tk
root = tk.Tk()
root.mainloop()

That will do it.这样就可以了。 Seeing as you want to add an image to the background, I would recommend using a canvas for holding all your *s.看到您想将图像添加到背景中,我建议使用画布来保存所有 * s。 The following will let you do that:以下将让您做到这一点:

import tkinter as tk

root = tk.Tk()

canvas = tk.Canvas(root)
canvas.pack()

for i, j in ((1,1),(10,10),(50,10),(100,1),(100,100)):
    canvas.create_text(i, j, text="*", anchor='nw')

root.mainloop()

I just picked some random places to put the *s, you can put them where ever you like.我只是随机挑选了一些放置 *s 的地方,你可以把它们放在任何你喜欢的地方。 Replace the i and j in my example with the locations you want them.将我示例中的 i 和 j 替换为您想要的位置。

After that, you can add images;之后,您可以添加图像; I recommend you look up how PIL works to do that.我建议您查看 PIL 如何做到这一点。 Also, seeing as you haven't gotten this far, maybe read up on tkinter's canvas tools as well.此外,鉴于您还没有走到这一步,也许还可以阅读 tkinter 的画布工具。 You can learn all sorts of things by typing in "pythong tkinter canvas " in to your favorite search engine.你可以通过在你最喜欢的搜索引擎中输入“pythong tkinter canvas”来学习各种各样的东西。

As I said, there are other ways to put text in a tkinter window.正如我所说,还有其他方法可以将文本放入 tkinter 窗口中。 Most notably, you could use the text widget or some label widgets.最值得注意的是,您可以使用文本小部件或一些标签小部件。 Might I recommend you start reading through http://effbot.org/tkinterbook/ and see what you can learn.我是否建议您开始阅读http://effbot.org/tkinterbook/ ,看看您能学到什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM