skip to Main Content

I’m building an Image to PDF converter app using python. When I run the code in the terminal the UI doesn’t appear. Here is the code:

`import tkinter as tk
 from tkinter import filedialog, messagebox
 import os

 class ImageToPdfConverter:
    def __init__(self, root):
        self.root = root
        self.image_paths = []
        self.output_pdf_name = tk.StringVar()
        self.selected_image_listbox = tk.Listbox(root, selectmode=tk.MULTIPLE)

        self.initialize_ui()

    def initialize_ui(self):
        title_label = tk.Label(self.root, text = "Image to PDF Converter", font=("Helvetica", 16,
        "bold"))
        title_label.pack(pady=10)

def main():
    root = tk.Tk()
    root.title("Image to PDF")
    root.geometry("400x600")
    root.mainloop()

    if __name__== "__main__":
        main()`

When I click "Run Python File in Terminal", it runs however the UI does not show up.

2

Answers


  1. The indentation of the main function at the end of the code is incorrect and should be changed to the following

    def main():
        root = tk.Tk()
        root.title("Image to PDF")
        root.geometry("400x600")
        root.mainloop()
    
    if __name__== "__main__":
        main()
    
    Login or Signup to reply.
  2. your code looks just fine but at the end the indentation of
    if __name__== "__main__": main()
    is incorrect.

    Here’s your fixed code that will generate the tkinter GUI:

    import tkinter as tk
    from tkinter import filedialog, messagebox
    import os
    
    class ImageToPdfConverter:
        def __init__(self, root):
            self.root = root
            self.image_paths = []
            self.output_pdf_name = tk.StringVar()
            self.selected_image_listbox = tk.Listbox(root, selectmode=tk.MULTIPLE)
    
            self.initialize_ui()
    
        def initialize_ui(self):
            title_label = tk.Label(self.root, text = "Image to PDF Converter", font=("Helvetica", 16,
            "bold"))
            title_label.pack(pady=10)
    
    def main():
        root = tk.Tk()
        root.title("Image to PDF")
        root.geometry("400x600")
        root.mainloop()
    
    if __name__== "__main__":
        main()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search