From 983dff8921c0706ced0e1e4346f6c9357afbe86b Mon Sep 17 00:00:00 2001 From: cxykevin Date: Sun, 21 Apr 2024 18:04:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=8C=E6=88=90scrollbar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- debug.py | 6 +- magictk/__init__.py | 1 + magictk/res.pickle | Bin 21933 -> 22329 bytes magictk/scrollbar.py | 207 ++++++++++++++++++++++++++++++++ photo2/res/scrollbarborder.json | 26 ++++ 5 files changed, 239 insertions(+), 1 deletion(-) create mode 100644 magictk/scrollbar.py create mode 100644 photo2/res/scrollbarborder.json diff --git a/debug.py b/debug.py index 75b2245..acd768f 100644 --- a/debug.py +++ b/debug.py @@ -82,7 +82,11 @@ if __name__ == "__main__": frame6.pack() frame7 = magictk.Frame(win) - magictk.Entry(frame7, w=100).pack(side='left') + magictk.Entry(frame7, w=200).pack(side='left') frame7.pack() + frame8 = magictk.Frame(win) + magictk.ScrollBar(frame8, h=200, allh=100).pack(side='left') + frame8.pack() + win.mainloop() diff --git a/magictk/__init__.py b/magictk/__init__.py index 41f998f..b95bf43 100644 --- a/magictk/__init__.py +++ b/magictk/__init__.py @@ -7,3 +7,4 @@ from magictk.submenu import Menu, MenuObjs from magictk.select import Select from magictk.frame import Frame from magictk.entry import Entry +from magictk.scrollbar import ScrollBar diff --git a/magictk/res.pickle b/magictk/res.pickle index 91bc1f1f730bd1df61e15086e0cba70077b98dba..3bb3f39b456b577eb7c13615d5f4dbf82155ddc8 100644 GIT binary patch delta 248 zcmZ3xnsMhkM%D(Fse0iXSy%82^A{%<<>%xiB^D*+7p0^YO>rujD6cnJk6)O@v(m?L z@?Ac8v5-(>A4dakPiMz;D6aGi37veLM={(NQ?49MEvftpzmQ~*+0MRMp2aSK`7k@v TJzX40= self.scroll_y and event.y <= self.scroll_y+self.bar_len): + pass + else: + self.scroll_y = min(self.h-self.bar_len, + max(0, event.y-self.bar_len//2)) + self.move_start = event.y-self.scroll_y + for i in range(len(self.__fill_obj["upside"])): + self.canvas.itemconfig( + self.__fill_obj["upside"][i], fill=color_tmpl.mix_color( + self.color["background"], self.color["border_base"], int((1-self.border_info[self.__fill_obj["upside_pos2"][i][0]][self.__fill_obj["upside_pos2"][i][1]]/255)*1000)/1000)) + self.canvas.itemconfig( + self.__fill_obj["fgbar"][0], fill=self.color["border_base"]) + for i in range(len(self.__fill_obj["bottomside"])): + self.canvas.itemconfig( + self.__fill_obj["bottomside"][i], fill=color_tmpl.mix_color( + self.color["background"], self.color["border_base"], int((1-self.border_info[self.__fill_obj["bottomside_pos2"][i][0]][self.__fill_obj["bottomside_pos2"][i][1]]/255)*1000)/1000)) + self.update_pos() + + def mouse_move(event: tkinter.Event): + delta_y = event.y-self.scroll_y + self.scroll_y = min(self.h-self.bar_len, + max(0, self.scroll_y+delta_y-self.move_start)) + print(self.get()) + self.update_pos() + + def mouse_release(event: tkinter.Event): + for i in range(len(self.__fill_obj["upside"])): + self.canvas.itemconfig( + self.__fill_obj["upside"][i], fill=color_tmpl.mix_color( + self.color["background"], self.color["border_light"], int((1-self.border_info[self.__fill_obj["upside_pos2"][i][0]][self.__fill_obj["upside_pos2"][i][1]]/255)*1000)/1000)) + self.canvas.itemconfig( + self.__fill_obj["fgbar"][0], fill=self.color["border_light"]) + for i in range(len(self.__fill_obj["bottomside"])): + self.canvas.itemconfig( + self.__fill_obj["bottomside"][i], fill=color_tmpl.mix_color( + self.color["background"], self.color["border_light"], int((1-self.border_info[self.__fill_obj["bottomside_pos2"][i][0]][self.__fill_obj["bottomside_pos2"][i][1]]/255)*1000)/1000)) + + self.canvas.bind("", mouse_move) + self.canvas.bind("", mouse_press) + self.canvas.bind("", mouse_release) + + def __draw(self): + self.__draw_corner(0, 0, 0, self.scroll_y, + self.color["border_light"], "upside", bgcolor=self.color["background"]) + self.__fill_obj["fgbar"] = [ + self.canvas.create_rectangle(1, 4+self.scroll_y, self.w-1, self.scroll_y+self.bar_len-4+1, width=0, fill=self.color["border_light"])] + self.__draw_corner(0, 1, 0, self.scroll_y+self.bar_len-4+1, + self.color["border_light"], "bottomside", bgcolor=self.color["background"]) + + def __update_pixel(self): + self.__move_progress_pixel = max(4, int((self.w-6)*self.progress)) + + def add_progress(self, n): + self.progress = max(min(self.progress+n, 1.0), 0.0) + self.__update_pixel() + + def set_progress(self, n): + self.progress = max(min(n, 1.0), 0.0) + self.__update_pixel() + + def bind_anim(self): + def anim_magictk(): + if (int(self.__progress_pixel) < int(self.__move_progress_pixel)): + if (self.__move_progress_pixel-self.__progress_pixel > 8): + self.__progress_pixel += 8 + else: + add_n = int( + (self.__progress_pixel-self.__move_progress_pixel)/2) + if (add_n <= 2): + self.__progress_pixel = self.__move_progress_pixel + else: + self.__progress_pixel += add_n + self.__update_prog() + elif (int(self.__progress_pixel) > int(self.__move_progress_pixel)): + if (self.__progress_pixel-self.__move_progress_pixel > 8): + self.__progress_pixel -= 8 + else: + add_n = int( + (self.__progress_pixel-self.__move_progress_pixel)/2) + if (add_n <= 2): + self.__progress_pixel = self.__move_progress_pixel + else: + self.__progress_pixel -= add_n + self.__update_prog() + + def anim_normal(*args): + self.__root.after(anim_normal, 16) + + try: + self.__root.anim == 0 + except: + self.__root.after(anim_normal, 16) + else: + if (anim_magictk not in self.__root.anim): + self.__root.anim.append(anim_magictk) + self.__anim_obj_id = self.__root.anim[-1] + + def get(self): + if (self.scroll_y+self.bar_len >= self.h): + return self._r_allh + else: + return int(min(self.scroll_y*self.y_size, self._r_allh)) diff --git a/photo2/res/scrollbarborder.json b/photo2/res/scrollbarborder.json new file mode 100644 index 0000000..93d6dd8 --- /dev/null +++ b/photo2/res/scrollbarborder.json @@ -0,0 +1,26 @@ +[ + [ + 255, + 157, + 22, + 22, + 157, + 255 + ], + [ + 157, + 0, + 0, + 0, + 0, + 157 + ], + [ + 22, + 0, + 0, + 0, + 0, + 22 + ] +] \ No newline at end of file