python-3.x - 绘制形状代替使用图像
问题描述
所以我有一个类,它显示一些带有背景的文本和一个与之相伴的形状图像。我想在这个类中创建/绘制形状,而不是使用实际的图像文件。我发现了一些绘制圆圈/菱形的代码,但我在课堂上实现它时遇到了麻烦。有什么方法可以做到这一点?
这是我的课:
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 - 运行代码后,我的 while 循环回到顶部。我怎样才能让它在 'again; 之后再次启动?
- linux - 在 Linux 中使用带有 FT4232H 的 libMPSSE
- json - 如何使用 JSONata 按键对 JSON 对象进行分组
- mysql - MySQL 8.0+ 用户'root'@'localhost'(使用密码:NO)的访问被拒绝
- html - 如何在媒体查询中重新排列图像/文本网格?
- r - R,ggplot:在 2 个 x 轴因子之间更改线型
- google-cloud-firestore - Firestore 模拟器:使用主机 0.0.0.0 在 UI 中看不到文档
- python - 受限制环境中的 Django 测试数据库
- java - 如何创建 Maven 依赖项?
- uiview - 在 SwiftUI 聊天应用程序中单击发送按钮时,如何清除自定义 UITextView 中的文本?