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

时间:2019-07-17 18:12:04

标签: python canvas tkinter

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

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

所有应用程序都很长,因此我将尝试显示我的代码。这是Tkinter课。

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()中调用此类的方式。 main中还有其他一些内容,因此我将只编写有关Tkinter的代码。假设用户的信息(名称,标题和图像路径正在传递到Tkinter类。)

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

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

0 个答案:

没有答案