当前位置: 首页>>代码示例>>Python>>正文


Python Canvas.winfo_width方法代码示例

本文整理汇总了Python中tkinter.Canvas.winfo_width方法的典型用法代码示例。如果您正苦于以下问题:Python Canvas.winfo_width方法的具体用法?Python Canvas.winfo_width怎么用?Python Canvas.winfo_width使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tkinter.Canvas的用法示例。


在下文中一共展示了Canvas.winfo_width方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: draw

# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import winfo_width [as 别名]
    def draw(self, canvas: tk.Canvas, points: [point.Point]) -> None:
        '''Draws a dot onto a canvas, given its points'''
        
        self._tl, self._br = points
        self._tl_x, self._tl_y = self._tl.frac()
        self._br_x, self._br_y = self._br.frac()

        width = canvas.winfo_width()
        height = canvas.winfo_height()
        point_pixels = []
        for point in points:
            point_pixels.append(point.pixel(width, height))

        canvas.create_rectangle(point_pixels[0][0],
                           point_pixels[0][1],
                           point_pixels[1][0],
                           point_pixels[1][1],
                           fill = 'black')
开发者ID:nhwangbo,项目名称:dots-and-boxes,代码行数:20,代码来源:dot.py

示例2: Application

# 需要导入模块: from tkinter import Canvas [as 别名]
# 或者: from tkinter.Canvas import winfo_width [as 别名]
class Application(Tk):
    def __init__(self):
        super().__init__()
        self.title('Hangman')
        self.geometry('640x480+200+200')
        self.resizable(width=False, height=False)

        menu_bar = Menu(self, tearoff=0)
        self.config(menu=menu_bar)

        game_menu = Menu(menu_bar, tearoff=0)
        game_menu.add_command(label='New game', command=self.__on_new_game)
        game_menu.add_command(label='Quit', command=self.__on_quit)
        menu_bar.add_cascade(label='Game', menu=game_menu)

        self.__hang_canvas = Canvas(self, background='#ffffff')
        self.__hang_canvas.pack(fill=BOTH, expand=True)

        self.__dict_filename = os.path.join(os.path.dirname(__file__), 'dictionary.txt')
        self.__dictionary = []
        self.__secret_word_masked = StringVar()
        self.__game_stages = (
            # (is_circle, x1, y1, x2, y2),
            (False, 50, 300, 200, 300), # podstawa
            (False, 60, 300, 60, 10), # maszt
            (False, 60, 10, 160, 10),
            (False, 60, 30, 100, 10),
            (False, 160, 10, 160, 30),
            (True, 140, 30, 180, 70),
            (False, 160, 70, 160, 200), # tułów
            (False, 140, 120, 160, 70), # lewa reka
            (False, 180, 120, 160, 70), # prawa ręka
            (False, 160, 200, 140, 250),
            (False, 160, 200, 180, 250)
        )
        self.__errors_count = 0

        secret_word_label = Label(self, font=('purisa', 36), textvariable=self.__secret_word_masked)
        secret_word_label.pack(fill=X)

        # user input panel
        Label(self, text='Please input a char:').pack(fill=X)
        self.__user_input_char = StringVar()
        user_input_entry = Entry(self, textvariable=self.__user_input_char)
        user_input_entry.bind('<Return>', self.__on_user_input)
        user_input_entry.pack()

        self.__load_dictionary()
        self.__new_game()

    def __on_user_input(self, event):
        text = self.__user_input_char.get()
        if text:
            c = text[0]
            if c in self.__secret_word:
                self.__secret_word_masked.set(str(self.__secret_word))
                # check is it victory
                if self.__secret_word.is_reveled():
                    self.__do_victory()
            else:
                # check if defeat
                self.__errors_count += 1
                self.__redraw_canvas()
                if self.__errors_count >= len(self.__game_stages):
                    self.__do_defeat()
        self.__user_input_char.set('')

    def __redraw_canvas(self):
        if self.__errors_count <= len(self.__game_stages):
            self.__hang_canvas.delete('all')
            for i in range(self.__errors_count):
                is_circle, x1, y1, x2, y2 = self.__game_stages[i]
                if is_circle:
                    self.__hang_canvas.create_oval(x1, y1, x2, y2)
                else:
                    self.__hang_canvas.create_line(x1, y1, x2, y2)

    def __do_defeat(self):
        pos = (self.__hang_canvas.winfo_width()/2, self.__hang_canvas.winfo_height()/2)
        self.__hang_canvas.create_text(pos, text='Defeat!', fill='#FF0000', font=('purisa', 36))
        print('Defeat!')
        self.__secret_word_masked.set(self.__secret_word.word)


    def __do_victory(self):
        pos = (self.__hang_canvas.winfo_width()/2, self.__hang_canvas.winfo_height()/2)
        self.__hang_canvas.create_text(pos, text='Victory!', fill='#FF0000', font=('purisa', 36))
        print('victory!')

    def __load_dictionary(self):
        with open(self.__dict_filename, 'r') as f:
            for line in f:
                self.__dictionary.append(line.lower().strip())

    def __on_new_game(self):
        self.__new_game()

    def __new_game(self):
        # Reset state of game
        word = random.choice(self.__dictionary)
#.........这里部分代码省略.........
开发者ID:4544fa8d,项目名称:Hangman,代码行数:103,代码来源:hangman.py


注:本文中的tkinter.Canvas.winfo_width方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。