读取RFID卡时如何在画布上显示图像?

2024-04-19 07:45:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为RFID阅读器构建GUI。我的目标是在已经有一些图像(如背景图像、公司徽标)的画布上显示用户的图像和名称2秒钟,这些图像是用canvas.create_textcanvas.create_image创建的

我知道我应该保存图像作为对对象的引用,或者将它们保存在一个列表中。但我无法处理的是在画布上显示一个包含用户图像和名称的框架或画布。当然,它应该为不同的用户显示不同的图像和名称。你知道吗

所有的应用程序都很长,所以我会尝试显示我的代码。这是特金特班。你知道吗

class GUI():
    def __init__(self, configs, settings, master_root):
        self.configs=configs
        self.settings=settings
        self.root = master_root
        self.canvas = Canvas(self.root, width=480, height=800)
        self.canvas.pack()
        self.waiting_screen()

    def image_opener(self, filename):
        opened_image = Image.open(CURRENT_DIR + self.configs.Folders.Gui + filename)
        photo_image = ImageTk.PhotoImage(opened_image)
        return photo_image

    '''This creates background picture'''
    def _main_page(self):
        img = self.image_opener("main_page")
        self.canvas.img_main = img
        self.canvas.create_image(0, 0, image=self.canvas.img_main, anchor = NW, state = visibility)

    '''This creates company's logo'''
    def _logo(self, x, y, anc, visibility):
        img = self.image_opener("logo")
        self.canvas.img_logo = img
        self.canvas.create_image(x, y, image=self.canvas.img_logo, anchor = anc, state= visibility)

    '''This shows todays date'''
    def _date_and_day(self, x, y, anc, visibility ):
        today = format_date(datetime.datetime.now(),"dd MMMM", locale = 'en').upper()
        day = format_date(datetime.datetime.now(), "EEEE", locale = 'en').upper()
        self.canvas.create_text(x, y, fill= "white", font = "Corbel 36",   anchor = anc, state= NORMAL, text = today)
        self.canvas.create_text(x, y+55, fill= "white", font = "Corbel 36",   anchor = anc, state= NORMAL, text = day)

    ''This will show user's name, title and photo. Now name, title and photo is static. But will be dynamic.'''
    def _show user_(self,x=240, y=430, anc=CENTER, visibility=NORMAL):
        img = self.image_opener(self.configs.Gui.Images.AccessBackground)
        self.canvas.img_access = img        
        self.canvas.create_image(x, y, image=self.canvas.img_access, anchor = anc, state=visibility)
        #self.canvas.create_image(x, y, image = self.image_user, anchor = anc, state=visibility)
        self.canvas.create_text(x, y+120, fill= "white", font = "Corbel 18",   anchor = anc, state= visibility, text = "EXAMPLE_NAME")
        self.canvas.create_text(x, y+150, fill= "white", font = "Corbel 15",   anchor = anc, state= visibility, text = "EXAMPLE_TITLE")

   def show_gui(self):    
        today = self.settings.Weather.Today
        self._main_page()
        self._logo(x=40 ,y=25, anc=NW, visibility=NORMAL)
        self._date_and_day(x=40 ,y=120, anc=NW, visibility=NORMAL)
        self._user_background(x=240 ,y=430, anc=CENTER, visibility=NORMAL)

下面是如何在main()中调用这个类。还有一些其他的东西,以及在主要的,所以我会写只是关于Tkinter的代码。假设用户的信息(名称、标题和图像路径被传递给Tkinter类。)

def main():
    settings = read_settings()
    configs = get_configs()
    root = Tk()
    gui = GUI(configs, settings, root)   
    gui.show_gui()
    root.mainloop()

所以我真正困惑的是我该怎么办。我应该继续使用画布还是将代码更改为标签和框架。在文档中我读到标签也是用来显示图像的。如果我有任何代码示例的话,那将是非常好的。你知道吗


Tags: text图像imageselfimgsettingsdefcreate