0
我一直在製作一個簡單的程序來進行文件夾備份。控制檯版本完美無瑕,但在處理Tkinter版本時遇到了一個問題。我想以類似網格的方式顯示按鈕,每個按鈕將負責給定文件夾的備份,並且我希望每個「頁面」限制20個按鈕。我設法創建簡單的分頁,僅在增加頁碼時才起作用。當我嘗試做減少頁碼,控制檯會提示我一個錯誤:Tkinter元素和分頁
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\Programy\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "D:\Rzeczy Mariusza\Python\Backup_manager\main_gui.py", line 83, in
go_back_event_handler self.show_avaliable_directories()
File "D:\Rzeczy Mariusza\Python\Backup_manager\main_gui.py", line 72, in
show_avaliable_directories
self.active_directories[i].place(x=_x, y=_y)
File "D:\Programy\Python34\lib\tkinter\__init__.py", line 2019, in
place_configure + self._options(cnf, kw))
_tkinter.TclError: bad window path name ".37362480.37388656"
類代碼:
from tkinter import Label, StringVar
from tkinter import ttk
class MainGui():
def __init__(self, main_frame, avaliable_directories):
"""
Building main elements located in the main_frame.
Args:
main_frame: instance - module Tkinter class ttk.Frame.
avaliable_directories: dict - contains avaliable directories for back up.
"""
self.main_frame = main_frame
self.avaliable_directories = avaliable_directories
self.choose_backup_dir_name = StringVar()
self.style_1 = ttk.Style()
self.style_1.map('directory.TButton',
foreground=[('!disabled', '#%02x%02x%02x' %(0, 0, 0)), ('disabled', '#%02x%02x%02x' %(115, 115, 115))],
background=[('!disabled', '#%02x%02x%02x' %(40, 240, 240)), ('disabled', '#%02x%02x%02x' %(240, 240, 240))])
self.pagination = 1
self.button_directories = []
self.active_directories = []
for element in self.avaliable_directories:
temp = ttk.Button(
self.main_frame,
width=20,
style='directory.TButton',
text=element
)
self.button_directories.append(temp)
if len(self.button_directories) > 20:
self.go_back = ttk.Button(self.main_frame, width=2, text='<')
self.go_back.place(x=325, y=540)
self.go_back.bind('<Button-1>', self.go_back_event_handler)
self.go_forward = ttk.Button(self.main_frame, width=2, text='>')
self.go_forward.place(x=350, y=540)
self.go_forward.bind('<Button-1>', self.go_forward_event_handler)
self.show_avaliable_directories()
# Interesting part starts here.
def show_avaliable_directories(self):
for i, element in enumerate(self.button_directories[20*(self.pagination-1):20*self.pagination]):# Implementation of pagination.
_x = 20+200*(i//10)
_y = 20+40*(i%10)
element.place(x=_x, y=_y)
# Placed elements are assigned to new list.
# When discarded (bellow) all elements from the list are destroyed.
self.active_directories.append(element)
def clear_active_directories(self):
for element in self.active_directories:
element.destroy()
self.active_directories = []
def go_back_event_handler(self, event):
if self.pagination > 1:
self.pagination -= 1
self.clear_active_directories()
self.show_avaliable_directories()
def go_forward_event_handler(self, event):
limit = (len(self.button_directories)//20)+1
if self.pagination < limit:
self.pagination += 1
self.clear_active_directories()
self.show_avaliable_directories()
我不知道爲什麼,當我增加了頁碼它的工作原理,但它當我嘗試減少頁碼時不會。謝謝你的幫助。