gpt4 book ai didi

python-3.x - 如何在列表或字典中存储来自 Gtk.Calendar.get_date() 的日期和来自 Gtk.TextBuffer 的 python 中该日期的文本

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:02:10 26 4
gpt4 key购买 nike

我正在尝试在 python 中创建一个日历应用程序。至于现在我已经设法创建了 Gtk.window,一个有两页的 Gtk.Notebook(page1月历,第 2 页周历)添加到 Gtk.window。我已将 Gtk.Calendar 用于月份并来自信号 'day-selected' 我使用了一个函数来 get_date()。我还使用了 Gtk.TextViewGtk.TextBuffer 来输入文本(作为注释).

第一个问题:如何将日期与输入文本连接起来并将其存储在列表或字典中,以便稍后将其保存到文件中。

第二:我已经使用self.calendar.set_property("show-details", True) 激活了详细信息并定义了一个函数self.calendar.set_detail_func (self.cal_entry) 了解详情。

    def cal_entry(self, calendar, year, month, date):
if self.textbuffer is not None:

self.calendar.mark_day(self.day)

由于上述功能无法正常使用,如何使用注释(Gtk.TextBuffer 中的文本)标记日期。

下面是一段代码:

class Window(Gtk.ApplicationWindow):

def __init__(self, app):
super(Window, self).__init__(title="Live Calendar", application=app)
self.set_default_size(-1, 400)
self.set_resizable(False)
self.set_border_width(10)
self.create_notebook()
self.entry()
self.refresh_icon()

def refresh_icon(self):
#refreshing the icon of the app
print(get_filename())
subject_lines = read_file(subject)
index = [i for i in range(len(subject_lines)) \
if subject_lines[i].startswith("Icon=")][0]
subject_lines[index] = "Icon=" + get_filename() + "\n"
write_file(subject, subject_lines)
self.set_icon_from_file(get_filename())
timer = threading.Timer(60, self.refresh_icon)
timer.start()

def create_notebook(self):
# Open Buttton
self.openbutton = Gtk.Button("Open")
self.openbutton.set_tooltip_text("Open Notes")
self.openbutton.connect("clicked", self.on_open_clicked)

# Save Button
self.savebutton = Gtk.Button("Save")
self.savebutton.set_tooltip_text("Save Notes")
self.savebutton.connect("clicked", self.on_save_clicked)

# Header
self.header = Gtk.HeaderBar(title="Live Calendar")
self.header.set_property("show_close_button", True)
self.header.pack_start(self.openbutton)
self.header.pack_start(self.savebutton)
self.set_titlebar(self.header)

#page1 month calendar
self.page1 = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
self.calendar = Gtk.Calendar()
self.calendar.set_property("show-week-numbers", True)
self.calendar.set_detail_height_rows(2)
self.calendar.set_detail_width_chars(2)
self.calendar.set_property("show-details", True)
self.calendar.set_detail_func(self.cal_entry)
self.__connect_signals()
self.page1.add(self.calendar)

#note taking
self.sw = Gtk.ScrolledWindow()
self.sw.set_hexpand(True)
self.sw.set_vexpand(True)
self.textview = Gtk.TextView()
self.textview.set_editable(True)
self.textview.set_wrap_mode(Gtk.WrapMode.WORD_CHAR)
self.textbuffer = self.textview.get_buffer()

self.sw.add(self.textview)
self.page1.pack_start(self.sw, True, True, 0)

#page2 week calendar




......a lot of code.......





#create notebook
self.notebook = Gtk.Notebook()
self.notebook.set_tab_pos(0)
self.notebook.append_page(self.page1, Gtk.Label('Month'))
self.notebook.append_page(self.page2, Gtk.Label('Week'))

self.add(self.notebook)

def __connect_signals(self):
self.day_selected_handle = self.calendar.connect('day-selected', self.entry)

def entry(self, *args):
self.page1.remove(self.label)
self.year, self.month, self.day = self.calendar.get_date()
self.month = self.month + 1
self.entrydate = datetime.date(self.year, self.month, self.day)
self.notedate = self.entrydate.strftime("%d/%m/%y")

self.text = self.entrydate.strftime("%d/%m/%y write your notes here...")
self.label = Gtk.Label(self.text)
self.page1.pack_start(self.label, False, False, 0)
self.show_all()


def cal_entry(self, calendar, year, month, date):
if self.textbuffer is not None:

self.calendar.mark_day(self.day)

#save into file
def on_save_clicked(self, widget):
dialog = Gtk.FileChooserDialog("Save file", self,
Gtk.FileChooserAction.SAVE,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_SAVE, Gtk.ResponseType.OK))

response = dialog.run()
if response == Gtk.ResponseType.OK:
save_file = dialog.get_filename()
start_iter = self.textbuffer.get_start_iter()
end_iter = self.textbuffer.get_end_iter()
text = self.textbuffer.get_text(start_iter, end_iter, True)
with open(save_file, 'w') as f:
f.write(text)
elif response == Gtk.ResponseType.CANCEL:
dialog.destroy()

dialog.destroy()

# open and read the file
def on_open_clicked(self, widget):
dialog = Gtk.FileChooserDialog("Please choose a file", self,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))

filter_text = Gtk.FileFilter()
filter_text.set_name("Text files")
filter_text.add_mime_type("text/plain")
dialog.add_filter(filter_text)

response = dialog.run()
if response == Gtk.ResponseType.OK:
selected_file = dialog.get_filename()
with open(selected_file, 'r') as f:
data = f.read()
self.textbuffer.set_text(data)
elif response == Gtk.ResponseType.CANCEL:
dialog.destroy()

dialog.destroy()

def quitApp(self, par):
app.quit()


class Application(Gtk.Application):

def __init__(self):
super(Application, self).__init__()

def do_activate(self):
self.win = Window(self)
self.win.show_all()

def do_startup(self):
Gtk.Application.do_startup(self)


app = Application()
app.run(sys.argv)

我们将不胜感激。

提前致谢

最佳答案

对于第一个问题:取决于,但可能是由 y/m/d 的三元组索引的字典

d = dict()
d[(2019, 10, 1)] = "your notes..."

第二个问题: details 的工作方式是 cal_entry(self, calendar, year, month, day) 必须返回一个字符串以显示在日历中的日期下方。所以你必须从上面的字典中获取文本。你可以这样做:

def cal_entry(self, calendar, year, month, day):
self.calendar.mark_day(day)
return dict.get((year, month, day))

也许这个示例代码可以提供帮助:

#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class NotePopover(Gtk.Popover):
def __init__(self, app):
Gtk.Popover.__init__(self)

self.set_size_request(200, 300)

grid = Gtk.Grid()
grid.set_orientation(Gtk.Orientation.VERTICAL)
scrolled_window = Gtk.ScrolledWindow()
self.text_view = Gtk.TextView()
scrolled_window.add(self.text_view)
grid.add(scrolled_window)
button = Gtk.Button(label='Add')
grid.add(button)
grid.show_all()
self.add(grid)

scrolled_window.set_vexpand(True)
scrolled_window.set_hexpand(True)

button.connect('clicked', self.on_button_clicked)

self.app = app

def do_popup(self):
self.popup()

def on_button_clicked(self, widget):
year = app.win.calendar.get_property('year')
month = app.win.calendar.get_property('month')
day = app.win.calendar.get_property('day')

start_iter = self.text_view.get_buffer().get_start_iter()
end_iter = self.text_view.get_buffer().get_end_iter()
app.notes_dict[(year, month, day)] = self.text_view.get_buffer().get_text(start_iter, end_iter, True)

app.win.do_update()

self.text_view.get_buffer().set_text("")
self.popdown()

class Window(Gtk.Window):
def __init__(self, app):
Gtk.Window.__init__(self)
self.set_title('Test GtkCalendar')
self.set_default_size(400, 300)

header_bar = Gtk.HeaderBar()
header_bar.set_show_close_button(True)
self.set_titlebar(header_bar)

button = Gtk.Button(label="Add note")
button.get_style_context().add_class('suggested-action')
button.connect('clicked', self.on_button_clicked)
header_bar.pack_start(button)

grid = Gtk.Grid()
grid.set_orientation(Gtk.Orientation.VERTICAL)
self.calendar = Gtk.Calendar()
scrolled_window = Gtk.ScrolledWindow()
self.text_view = Gtk.TextView()

self.add(grid)
grid.add(self.calendar)
grid.add(scrolled_window)
scrolled_window.add(self.text_view)

self.calendar.set_hexpand(True)
self.calendar.set_halign(Gtk.Align.CENTER)
scrolled_window.set_vexpand(True)
scrolled_window.set_hexpand(True)

self.popover = NotePopover(app)
self.popover.set_relative_to(button)

self.connect('destroy', Gtk.main_quit)
self.calendar.connect('day-selected', self.calendar_changed)
self.calendar.connect('month-changed', self.calendar_changed)

self.show_all()

self.app = app

def on_button_clicked(self, widget):
self.popover.do_popup()

def calendar_changed(self, widget):
self.do_update()

def do_update(self):
year = app.win.calendar.get_property('year')
month = app.win.calendar.get_property('month')
day = app.win.calendar.get_property('day')

text = app.notes_dict.get((year, month, day))

if text is not None:
self.text_view.get_buffer().set_text(text)
else:
self.text_view.get_buffer().set_text("")

class Application:
def __init__(self):
self.notes_dict = dict()
self.win = Window(self)

if __name__ == '__main__':
app = Application()
Gtk.main()

关于python-3.x - 如何在列表或字典中存储来自 Gtk.Calendar.get_date() 的日期和来自 Gtk.TextBuffer 的 python 中该日期的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58188976/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com