|
| 1 | +import vlc |
| 2 | +import ttkbootstrap as ttk |
| 3 | +import tkinter as tk |
| 4 | +from tkinter import filedialog |
| 5 | + |
| 6 | +class CircleButton(tk.Canvas): |
| 7 | + def __init__(self, btn_name, master=None, x=50, y=50, radius=20, |
| 8 | + command=None, btn_text ='', text_size = None, color=(255, 255, 254), **kwargs): |
| 9 | + super().__init__(master, width=2*radius, height=2*radius, **kwargs) |
| 10 | + self.radius = radius |
| 11 | + self.text_size = text_size |
| 12 | + self.btn_name = btn_name |
| 13 | + self.btn_text = btn_text |
| 14 | + self.color = color |
| 15 | + self.radius = radius |
| 16 | + self.command = command |
| 17 | + self.oval = self.create_oval(2, 2, radius*2-2, radius*2-2, tags=self.btn_name, fill=self.__rgb_to_hex(self.color), |
| 18 | + outline = self.__rgb_to_hex(self.color), width=2) |
| 19 | + self.text = self.create_text(radius+1, radius+1, text=self.btn_text, anchor=tk.CENTER, tags=self.btn_name, |
| 20 | + font=("Helvetica", self.radius // 3 if self.text_size is None else self.text_size), |
| 21 | + fill='white') |
| 22 | + |
| 23 | + self.tag_bind(btn_name, "<ButtonPress-1>", self.__top_layer_mouse_dn) |
| 24 | + self.tag_bind(btn_name, "<ButtonRelease-1>", self.on_release) |
| 25 | + # self.configure(relief='groove', borderwidth=0) |
| 26 | + self.place(x=x, y=y) |
| 27 | + # self.tag_bind(self.btn_name, "<Button-1>", self.on_press) |
| 28 | + |
| 29 | + |
| 30 | + def __rgb_to_hex(self, rgb): |
| 31 | + return '#{:02x}{:02x}{:02x}'.format(rgb[0], rgb[1], rgb[2]) |
| 32 | + |
| 33 | + def __top_layer_mouse_dn(self, event): |
| 34 | + x, y = event.x, event.y |
| 35 | + closesttag = self.find_closest(event.x, event.y) |
| 36 | + tag_name = self.gettags(closesttag) |
| 37 | + |
| 38 | + # print(tag_name) |
| 39 | + pressed_color = (self.color[0]-20 if self.color[0] > 20 else self.color[0] , |
| 40 | + self.color[1]-20 if self.color[1] > 20 else self.color[1], |
| 41 | + self.color[2]-20 if self.color[2] > 20 else self.color[2]) |
| 42 | + |
| 43 | + self.itemconfig(self.oval, fill=self.__rgb_to_hex(pressed_color), |
| 44 | + outline=self.__rgb_to_hex(self.color), width=4) |
| 45 | + |
| 46 | + self.itemconfig(self.text, font=("Helvetica", |
| 47 | + (self.radius // 3 if self.text_size is None else self.text_size)-2)) |
| 48 | + self.move(self.text, 0, 0) |
| 49 | + |
| 50 | + def on_release(self, event): |
| 51 | + x, y = event.x, event.y |
| 52 | + closesttag = self.find_closest(event.x, event.y) |
| 53 | + tag_name = self.gettags(closesttag) |
| 54 | + |
| 55 | + self.itemconfig(self.oval, fill=self.__rgb_to_hex(self.color), |
| 56 | + outline=self.__rgb_to_hex(self.color), width=4) |
| 57 | + self.itemconfig(self.text, font=("Helvetica", self.radius // 3 if self.text_size is None else self.text_size)) |
| 58 | + self.move(self.text, -0, -0) |
| 59 | + if self.command is not None and len(tag_name)>1 and tag_name[1] == 'current': |
| 60 | + self.command() |
| 61 | + |
| 62 | +class Video: |
| 63 | + def __init__(self, canvas, player): |
| 64 | + self.canvas = canvas |
| 65 | + self.player = player |
| 66 | + |
| 67 | + def open_file(self): |
| 68 | + video_path = filedialog.askopenfilename(filetypes=[("Video Files", "*.mp4;*.avi;*.mkv")]) |
| 69 | + if video_path: |
| 70 | + media = self.player.get_instance().media_new(video_path) |
| 71 | + self.player.set_media(media) |
| 72 | + self.player.set_hwnd(self.canvas.winfo_id()) |
| 73 | + |
| 74 | + def play(self): |
| 75 | + self.player.play() |
| 76 | + |
| 77 | + def pause(self): |
| 78 | + self.player.pause() |
| 79 | + |
| 80 | + def rewind(self): |
| 81 | + self.player.set_time(self.player.get_time() - 10000) # Rewind 10 seconds |
| 82 | + |
| 83 | + def forward(self): |
| 84 | + self.player.set_time(self.player.get_time() + 10000) # Forward 10 seconds |
| 85 | + |
| 86 | + def volume_up(self): |
| 87 | + volume = self.player.audio_get_volume() |
| 88 | + self.player.audio_set_volume(volume + 10) # Increase volume by 10 |
| 89 | + |
| 90 | + def volume_down(self): |
| 91 | + volume = self.player.audio_get_volume() |
| 92 | + self.player.audio_set_volume(volume - 10) # Decrease volume by 10 |
| 93 | + |
| 94 | +class VideoPlayer: |
| 95 | + def __init__(self, root, canvas, btns_x=0, btns_y=0): |
| 96 | + self.root = root |
| 97 | + self.btns_x = btns_x |
| 98 | + self.btns_y = btns_y |
| 99 | + self.instance = vlc.Instance() |
| 100 | + self.player = self.instance.media_player_new() |
| 101 | + self.video = Video(canvas, self.player) |
| 102 | + self.canvas = canvas |
| 103 | + self.create_ui() |
| 104 | + |
| 105 | + def exit(self): |
| 106 | + self.player.stop() |
| 107 | + self.root.destroy() |
| 108 | + |
| 109 | + def create_ui(self): |
| 110 | + self.controls_frame = ttk.Frame(self.root, height=100, width=500) |
| 111 | + self.controls_frame.place(x=self.btns_x, y=self.btns_y) |
| 112 | + |
| 113 | + self.open_button = CircleButton('btnOpen' ,self.controls_frame,10, 0,30, btn_text='Open', |
| 114 | + color=(250, 110, 130), command=self.video.open_file) |
| 115 | + |
| 116 | + self.play_button = CircleButton('btnPlay' ,self.controls_frame,70, 0,30, btn_text='Play', |
| 117 | + color=(250, 110, 130), command=self.video.play) |
| 118 | + |
| 119 | + self.pause_button = CircleButton('btnPause' ,self.controls_frame,130, 0,30, |
| 120 | + btn_text='Pause', color=(250, 110, 130), command=self.video.pause) |
| 121 | + |
| 122 | + self.forward_button = CircleButton('btnForward' ,self.controls_frame,190, 0,30 |
| 123 | + , btn_text='Forward',color=(250, 110, 130), command=self.video.forward) |
| 124 | + |
| 125 | + self.rewind_button = CircleButton('btnRewind' ,self.controls_frame,250, 0,30 |
| 126 | + , btn_text='Rewind',color=(250, 110, 130), command=self.video.rewind) |
| 127 | + |
| 128 | + self.vol_dn_button = CircleButton('btnVol_dn' ,self.controls_frame,310, 0,30 |
| 129 | + , btn_text='Vol-',color=(250, 110, 130), command=self.video.volume_down) |
| 130 | + |
| 131 | + self.vol_up_button = CircleButton('btnVol_up' ,self.controls_frame,370, 0,30 |
| 132 | + , btn_text='Vol+',color=(250, 110, 130), command=self.video.volume_up) |
| 133 | + |
| 134 | + self.exit_button = CircleButton('btnExit' ,self.controls_frame,430, 0,30 |
| 135 | + , btn_text='Exit',color=(200,50,10), command=self.exit) |
| 136 | + |
| 137 | + |
| 138 | +if __name__ == "__main__": |
| 139 | + root = ttk.Window(themename="superhero") |
| 140 | + # root = tk.Tk() |
| 141 | + root.title("Video Player") |
| 142 | + root.geometry("700x800") |
| 143 | + |
| 144 | + canvas1 = tk.Canvas(root, bg='black') |
| 145 | + canvas1.place(x=0, y=0, width=700, height=650) |
| 146 | + |
| 147 | + player1 = VideoPlayer(root, canvas1, btns_x=80, btns_y=700) |
| 148 | + |
| 149 | + root.mainloop() |
0 commit comments