PyGTKDeprecation
von HelsAett- SNIPPET_TEXT:
-
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- #########################################################################
- # File Name: test.py
- # Author: debian
- # mail: debian@debian-virtualbox
- # Created Time: 2024-05-02 14:19:41
- #########################################################################
- import gi
- gi.require_version("Gtk", "3.0")
- from gi.repository import Gtk, Gdk, Gio
- import os
- import sys
- class PDFMergeWindow(Gtk.Window):
- def __init__(self):
- Gtk.Window.__init__(self, title="PDF Merge")
- self.set_default_size(400, 200)
- self.connect("destroy", Gtk.main_quit)
- self.selected_files = []
- self.file_chooser_button = Gtk.Button(label="Select PDF files")
- self.file_chooser_button.connect("clicked", self.on_file_chooser_clicked)
- self.file_chooser_button.set_hexpand(True)
- self.plus_button = Gtk.Button(label="+")
- self.plus_button.connect("clicked", self.on_plus_clicked)
- self.plus_button.set_hexpand(True)
- self.merge_button = Gtk.Button(label="Merge")
- self.merge_button.connect("clicked", self.on_merge_clicked)
- self.merge_button.set_hexpand(True)
- self.cancel_button = Gtk.Button(label="Cancel")
- self.cancel_button.connect("clicked", self.on_cancel_clicked)
- self.cancel_button.set_hexpand(True)
- self.file_list = Gtk.ListBox()
- self.file_list.set_selection_mode(Gtk.SelectionMode.NONE)
- self.file_list.set_hexpand(True)
- self.file_list.set_vexpand(True)
- self.scrollable_window = Gtk.ScrolledWindow()
- self.scrollable_window.add(self.file_list)
- self.grid = Gtk.Grid()
- self.grid.attach(self.file_chooser_button, 0, 0, 1, 1)
- self.grid.attach_next_to(self.plus_button, self.file_chooser_button, Gtk.PositionType.RIGHT, 1, 1)
- self.grid.attach_next_to(self.scrollable_window, self.file_chooser_button, Gtk.PositionType.BOTTOM, 2, 1)
- self.grid.attach_next_to(self.merge_button, self.scrollable_window, Gtk.PositionType.BOTTOM, 1, 1)
- self.grid.attach_next_to(self.cancel_button, self.merge_button, Gtk.PositionType.RIGHT, 1, 1)
- self.add(self.grid)
- # Add initial files to the list if provided as command line arguments
- if len(sys.argv) > 1:
- initial_files = sys.argv[1:]
- for filename in initial_files:
- self.add_file_to_list(filename)
- def on_file_chooser_clicked(self, button):
- dialog = Gtk.FileChooserDialog(
- "Please choose PDF files", self,
- Gtk.FileChooserAction.OPEN,
- (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
- Gtk.STOCK_OPEN, Gtk.ResponseType.OK)
- )
- dialog.set_select_multiple(True) # Allow multiple file selection
- dialog.add_filter(Gtk.FileFilter())
- dialog.get_filter().set_name("PDF files")
- dialog.get_filter().add_pattern("*.pdf")
- response = dialog.run()
- if response == Gtk.ResponseType.OK:
- for filename in dialog.get_filenames():
- self.add_file_to_list(filename)
- dialog.destroy()
- def add_file_to_list(self, filename):
- self.selected_files.append(filename)
- row = Gtk.ListBoxRow()
- hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6)
- label = Gtk.Label(label=filename, xalign=0) # Left align the label
- hbox.pack_start(label, True, True, 0)
- row.add(hbox)
- self.file_list.add(row)
- def on_plus_clicked(self, button):
- self.on_file_chooser_clicked(button)
- def on_merge_clicked(self, button):
- if len(self.selected_files) == 0:
- self.show_error_dialog("No PDF files selected.")
- return
- dialog = Gtk.FileChooserDialog(
- "Save merged PDF as...", self,
- Gtk.FileChooserAction.SAVE,
- (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
- Gtk.STOCK_SAVE, Gtk.ResponseType.OK)
- )
- dialog.add_filter(Gtk.FileFilter())
- dialog.get_filter().set_name("PDF files")
- dialog.get_filter().add_pattern("*.pdf")
- response = dialog.run()
- if response == Gtk.ResponseType.OK:
- output_file = dialog.get_filename()
- self.save_merged_pdf(output_file)
- dialog.destroy()
- def save_merged_pdf(self, output_file):
- if os.path.exists(output_file):
- response = self.show_overwrite_confirmation_dialog(output_file)
- if response == Gtk.ResponseType.NO:
- return
- self.merge_pdfs(output_file)
- def merge_pdfs(self, output_file):
- cmd = ["pdfunite"] + self.selected_files + [output_file]
- if result == 0:
- self.show_info_dialog(f"PDFs merged successfully. Output file: {output_file}")
- Gtk.main_quit()
- else:
- self.show_error_dialog("Failed to merge PDFs.")
- def on_cancel_clicked(self, button):
- Gtk.main_quit()
- def show_error_dialog(self, message):
- dialog = Gtk.MessageDialog(
- transient_for=self,
- flags=0,
- message_type=Gtk.MessageType.ERROR,
- buttons=Gtk.ButtonsType.OK,
- text=message
- )
- dialog.run()
- dialog.destroy()
- def show_info_dialog(self, message):
- dialog = Gtk.MessageDialog(
- transient_for=self,
- flags=0,
- message_type=Gtk.MessageType.INFO,
- buttons=Gtk.ButtonsType.OK,
- text=message
- )
- dialog.run()
- dialog.destroy()
- def show_overwrite_confirmation_dialog(self, output_file):
- dialog = Gtk.MessageDialog(
- transient_for=self,
- flags=0,
- message_type=Gtk.MessageType.WARNING,
- buttons=Gtk.ButtonsType.YES_NO,
- text=f"File '{output_file}' already exists. Do you want to overwrite it?"
- )
- response = dialog.run()
- dialog.destroy()
- return response
- # Beispiel für die Verwendung:
- win = PDFMergeWindow()
- win.show_all()
- Gtk.main()
Quellcode
Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN