首页 > 解决方案 > 绘制形状代替使用图像

问题描述

所以我有一个类,它显示一些带有背景的文本和一个与之相伴的形状图像。我想在这个类中创建/绘制形状,而不是使用实际的图像文件。我发现了一些绘制圆圈/菱形的代码,但我在课堂上实现它时遇到了麻烦。有什么方法可以做到这一点?

这是我的课:

from tkinter import Tk, Button, Label, BOTTOM, CENTER
from PIL import ImageTk, Image

# set properties of window
window_width = 1920
window_height = 238

# set properties of image
# NOTE: pixel dims of image file mapped 1 to 1 on window
image_width = 238
image_height = 238

# set properties of label
label_width = window_width - image_width
label_height = 238
background_color = 'navy'
foreground_color = 'yellow'

# set properties of font
font_family = 'Helvetica'
font_height = 110
font_weight = 'bold'

# variables for text switching
testIndex = 0
text = [
    'Test Test Test',
    'TEST TEST !!!',
        '12345678901234567890'
]

class GUIClass:
    def __init__(self, root):
        # bringing text variables into class scope
        self.root = root
        self.testIndex = testIndex
        self.text = text

        # creating image for sign
        self.image = ImageTk.PhotoImage(Image.open("testImage.png"))
        self.imageLabel = Label(image=self.image, width=image_width, height=image_height, bg=background_color)
        self.imageLabel.place(x=0, y=0, width=image_width, height=image_height)
    
        # creating label, setting font, position, and binding left-click event listener
        self.label = Label(root, text=text[testIndex], bg=background_color, fg=foreground_color, borderwidth=10, relief="solid")
        self.label['font'] = (font_family, font_height, font_weight)
        self.label.bind("<Button-1>", self.closeScreen)
        self.label.place(x=image_width, y=0, width=label_width, height=window_height)

        # creating close button to close window (necessary if removing border of window)
        #self.close_button = Button(root, text='close', command= root.quit)
        #self.close_button.pack(side=BOTTOM)

        #set timer to cycle through strings
        self.root.after(1500, self.timerTest)

    # threaded event handler for when timer elapses
    def timerTest(self):
        self.testIndex = self.testIndex + 1
        self.testIndex %= len(self.text)
        self.label['text'] = self.text[self.testIndex]

        self.root.after(1500, self.timerTest)
    
    # closes window when label is left-clicked
    def closeScreen(self, event):
        self.root.quit()


# creates the window
root = Tk()

# sets dimensions of window (ex: "200x200")
root.geometry(str(window_width) + "x" + str(window_height))

# removes boundary, disables resizing, and removes buttons of window
root.overrideredirect(True)

# attaches the window to the GUI class
rootGUI = GUIClass(root)

# continously loops, draws display, and waits for user input
root.mainloop()

这是我为圆形/菱形找到的代码:

from tkinter import Tk, Canvas, Frame, BOTH

class Example(Frame):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):

        self.master.title("Shapes")
        self.pack(fill=BOTH, expand=1)

        canvas = Canvas(self)

        canvas.create_oval(5, 5, 233, 233, outline="#fb0", fill="#fb0", width=0)

        points = [119, 5, 233, 119, 119, 233, 5, 119]
        canvas.create_polygon(points, outline='#fb0', fill='#fb0', width=0)

        canvas.pack(fill=BOTH, expand=1)


def main():

    root = Tk()
    ex = Example()
    root.geometry("330x220+300+300")
    root.mainloop()


if __name__ == '__main__':
    main()

标签: python-3.xtkinter

解决方案


推荐阅读