如何使用OOP确保所有元素都显示在Tkinter画布上

ushi

我对python相当陌生,并试图掌握其类和函数。我之前编写了代码,为了使代码整洁,我尝试将其放入类中,但是其中某些元素不再显示。

该代码应该显示一个带有文本和按钮的白色圆角矩形,效果很好,但是当我添加class时,按钮和文本不会显示。

这可能只是很小的东西,但是正如我所说的,我试图学习-如何使其像使用OOP之前那样显示?我究竟做错了什么?谢谢您的帮助!

我的代码如下。

    root = Tk()
root.configure(bg="steel blue")
canvas = Canvas(root, highlightthickness=0)
canvas.config(width=350, height=250,
             bg="steel blue", )  # canvas.config(width = root.winfo_screenwidth(), height = root.winfo_screenheight() )
canvas.pack()


class User_Identification(object):
    def User_Id(self):
        canvas.delete("all")  # root.configure(bg = "white")   #canvas.config(bg = "white")
        canvas.config(width=505, height=505,
                      bg="steel blue")  # canvas.config(width = root.winfo_screenwidth(), height = root.winfo_screenheight() )
        box = canvas.create_rectangle(40, 20, 500, 500, fill="white", width=4)
        canvas.create_text(255, 65, text="Who are you?", font=("comic sans", 30))
        box2 = canvas.create_rectangle(50, 30, 450, 100, width=2)
        canvas.move(box2, 9, 5)
        canvas.place(relx=0.5, rely=0.5, anchor=CENTER)

        User_button = Button(root, text="User", anchor=CENTER, command=Per_Form)
        User_button.configure(width=45, height=6, bg="white", fg="black", border=10)  # height
        User_button = canvas.create_window(60, 150, anchor=NW, window=User_button)

        Driver_button = Button(root, text="Driver", anchor=CENTER, command=Per_Form)
        Driver_button.configure(width=45, height=6, bg="white", fg="black", border=10)  # height
        Driver_button = canvas.create_window(60, 300, anchor=NW, window=Driver_button)


class Rectangle:

   def round_rectangle(x1, y1, x2, y2, radius=25, **kwargs):  # Making of the round rectangle

       points = [x1 + radius, y1,
                 x1 + radius, y1,
                 x2 - radius, y1,
                 x2 - radius, y1,
                 x2, y1,
                 x2, y1 + radius,
                 x2, y1 + radius,
                 x2, y2 - radius,
                 x2, y2 - radius,
                 x2, y2,
                 x2 - radius, y2,
                 x2 - radius, y2,
                 x1 + radius, y2,
                 x1 + radius, y2,
                 x1, y2,
                 x1, y2 - radius,
                 x1, y2 - radius,
                 x1, y1 + radius,
                 x1, y1 + radius,
                 x1, y1]

       return canvas.create_polygon(points, **kwargs, smooth=True)

   my_rectangle = round_rectangle(10, 60, 330, 250, radius=23, outline="black", fill="white", width=4)

   def __init__(self, Next_button):
        self.Next_button = Next_button
        Next_button = Button(root, text="Next", anchor=CENTER, command=User_Identification)
        Next_button.configure(width=10, bg="black", fg="blue", border=10)
        canvas.create_window(500, 500, anchor=NW, window=Next_button)

   def text(text):
        text = canvas.create_text(170, 100, text="Hey there, welcome to DeliverToday! \nWe bring your needs right at your doorstep ~ \nhave a great journey ahead!")


canvas.place(relx=0.5, rely=0.5, anchor=CENTER)
迈克尔·吉德利

我认为您正在尝试执行以下脚本。我举例说明了许多不同的概念,其中一些对于您的需求是不必要的。阅读文档并学习计算机科学,以便您可以理解它们。

config.py

from dataclasses import dataclass, asdict

#to make things more complicated and force you to get better
#create a dataclass of default properties
@dataclass
class Button_dc:
    bg:    str = "white"
    fg:    str = "gray20"
    font:  str = 'Calibri 14 bold'
    border:int = 10
    
#make a dict instance of the dataclass    
DefaultButton = asdict(Button_dc())

#make a slightly different instance
UserRoleButton = asdict(Button_dc(font='Calibri 24 bold'))

shapes.py

#add more static methods that return complex shape points
class Shapes:
    @staticmethod
    def RoundRectangle(x1, y1, x2, y2, radius=25):  # Making of the round rectangle
        return [x1 + radius, y1,
                x1 + radius, y1,
                x2 - radius, y1,
                x2 - radius, y1,
                x2, y1,
                x2, y1 + radius,
                x2, y1 + radius,
                x2, y2 - radius,
                x2, y2 - radius,
                x2, y2,
                x2 - radius, y2,
                x2 - radius, y2,
                x1 + radius, y2,
                x1 + radius, y2,
                x1, y2,
                x1, y2 - radius,
                x1, y2 - radius,
                x1, y1 + radius,
                x1, y1 + radius,
                x1, y1]

main.py

import tkinter as tk
import config as cfg
from shapes import Shapes
from enum import Enum


#entirely over-engineered for it's purpose
class Role(Enum):
    User = 1
    Driver = 2


class App(tk.Tk):
    #width, height and title are constants of your app so, write them as such
    WIDTH  = 660
    HEIGHT = 400
    TITLE  = "DeliveryToday ~ We bring your needs right to your doorstep"

    def __init__(self):
        tk.Tk.__init__(self)
        self.configure(bg="steel blue")

        self.canvas = tk.Canvas(self, bg="steel blue", highlightthickness=0)
        self.canvas.pack(side='top', fill='both', expand=True)
        
        rect = Shapes.RoundRectangle(20, 20, 640, 320, radius=60)
        self.canvas.create_polygon(rect, outline="black", fill="white", width=4, smooth=True)

        #simple manual text centering, also...
        #making an entire function to store text is ridiculous
        text = f'{"Hey there, welcome to DeliverToday!":^{48}}\n{"We bring your needs right to your doorstep.":^{40}}\n{"Have a great journey ahead!":^{46}}'
        self.canvas.create_text(40, 100, anchor='nw', font='Helvetica 20 bold', text=text)
        
        #using **kwargs makes lines a lot shorter. prepare your data, then use it
        self.next_btn = tk.Button(self, text="next", command=self.user_id, **cfg.DefaultButton)
        self.canvas.create_window(500, 340, anchor='nw', window=self.next_btn)
        
    def user_id(self):
        self.canvas.delete("all")
        #put user id code here
        self.canvas.create_rectangle(40, 20, 620, 380, fill="white", width=4)
        self.canvas.create_text(255, 65, text="Choose your role?", font="comicsans 30")
        
        #using a lambda in this way we can pass arguments to other methods
        self.user_btn = tk.Button(self, text="User", command=lambda: self.user_form(Role.User), **cfg.UserRoleButton)
        self.canvas.create_window(60, 280, anchor='nw', window=self.user_btn)
        
        self.driver_btn = tk.Button(self, text="Driver", command=lambda: self.user_form(Role.Driver), **cfg.UserRoleButton)
        self.canvas.create_window(180, 280, anchor='nw', window=self.driver_btn)
        
    def user_form(self, type):
        if type is Role.User:
            print("this is a user")
        elif type is Role.Driver:
            print("this is a driver")
        

#use proper PEP8 to initialize your program
if __name__ == "__main__":
    app = App()
    app.geometry(f'{App.WIDTH}x{App.HEIGHT}')
    app.resizable(width=False, height=False)
    app.title(App.TITLE)
    app.mainloop()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python-如何确保嵌套列表中元素的所有长度都相同?

来自分类Dev

如何确保以前在PC上删除的所有内容都无法恢复?

来自分类Dev

如何使用带有 OOP 的枕头在 tkinter 中显示图像

来自分类Dev

如何在 tkinter 上创建后退按钮/或删除画布上的所有小部件?

来自分类Dev

使用排序方法逻辑时,并非所有数组元素都显示

来自分类Dev

AngularJS:所有使用指令的元素都显示相同的值

来自分类Dev

如何在iOS顶部的所有UI元素都绘制在整个屏幕上?

来自分类Dev

如何实现多个选择列表,以确保所有人都只能使用唯一的值?

来自分类Dev

Python,Tkinter:如何使用其ID或标签获取所有画布对象的句柄?

来自分类Dev

HTML画布使所有内容都变粗

来自分类Dev

所有的精灵表都显示在屏幕上

来自分类Dev

如何使用检查元素工具更改所有显示状态?

来自分类Dev

如何显示所有使用jQuery隐藏的元素?

来自分类Dev

如何确保模块中的所有方法都具有相同的签名?

来自分类Dev

Tensorflow:如何确保每批中的所有样本都具有不同的标签?

来自分类Dev

当我在数组上使用追加操作时,所有元素都具有相同的值

来自分类Dev

如何确保在同一函数终止之前在函数中创建的所有线程都返回?

来自分类Dev

如何确保所有事情都完成了,或者什么都不做?

来自分类Dev

外部磁盘-如何确保所有人都无需root权限即可读取?

来自分类Dev

如何让所有评论类型都显示在 WordPress 中?

来自分类Dev

如果所有li子元素都具有CSS样式显示,则隐藏父元素:无

来自分类Dev

pdfjs:如何创建多个画布以显示所有页面?

来自分类Dev

pdfjs:如何创建多个画布以显示所有页面?

来自分类Dev

如何使用类在tkinter画布上绘制多边形?

来自分类Dev

如何选择画布上的所有节点(sigma.js)

来自分类Dev

我在Tomcat上的所有内容都超时了,日志显示全部为404。我该如何调试呢?

来自分类Dev

使用Fabric.js选择画布上的所有对象

来自分类Dev

使用画布形状名称在鼠标悬停画布形状上突出显示 HTML 元素

来自分类Dev

如何快速显示数组的所有元素?

Related 相关文章

  1. 1

    Python-如何确保嵌套列表中元素的所有长度都相同?

  2. 2

    如何确保以前在PC上删除的所有内容都无法恢复?

  3. 3

    如何使用带有 OOP 的枕头在 tkinter 中显示图像

  4. 4

    如何在 tkinter 上创建后退按钮/或删除画布上的所有小部件?

  5. 5

    使用排序方法逻辑时,并非所有数组元素都显示

  6. 6

    AngularJS:所有使用指令的元素都显示相同的值

  7. 7

    如何在iOS顶部的所有UI元素都绘制在整个屏幕上?

  8. 8

    如何实现多个选择列表,以确保所有人都只能使用唯一的值?

  9. 9

    Python,Tkinter:如何使用其ID或标签获取所有画布对象的句柄?

  10. 10

    HTML画布使所有内容都变粗

  11. 11

    所有的精灵表都显示在屏幕上

  12. 12

    如何使用检查元素工具更改所有显示状态?

  13. 13

    如何显示所有使用jQuery隐藏的元素?

  14. 14

    如何确保模块中的所有方法都具有相同的签名?

  15. 15

    Tensorflow:如何确保每批中的所有样本都具有不同的标签?

  16. 16

    当我在数组上使用追加操作时,所有元素都具有相同的值

  17. 17

    如何确保在同一函数终止之前在函数中创建的所有线程都返回?

  18. 18

    如何确保所有事情都完成了,或者什么都不做?

  19. 19

    外部磁盘-如何确保所有人都无需root权限即可读取?

  20. 20

    如何让所有评论类型都显示在 WordPress 中?

  21. 21

    如果所有li子元素都具有CSS样式显示,则隐藏父元素:无

  22. 22

    pdfjs:如何创建多个画布以显示所有页面?

  23. 23

    pdfjs:如何创建多个画布以显示所有页面?

  24. 24

    如何使用类在tkinter画布上绘制多边形?

  25. 25

    如何选择画布上的所有节点(sigma.js)

  26. 26

    我在Tomcat上的所有内容都超时了,日志显示全部为404。我该如何调试呢?

  27. 27

    使用Fabric.js选择画布上的所有对象

  28. 28

    使用画布形状名称在鼠标悬停画布形状上突出显示 HTML 元素

  29. 29

    如何快速显示数组的所有元素?

热门标签

归档