splash screen problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nazish zehra
    New Member
    • Dec 2011
    • 31

    splash screen problem

    i am using splash screen for my program but problem is that i want the canvas to be displayed in small size and in the centre of screen.if i adjust the size then it moves away from the centre of screen.Code is:

    Code:
    import Tkinter as tk
     
    root = tk.Tk()
    # show no frame
    root.overrideredirect(True)
    width = root.winfo_screenwidth()
    height = root.winfo_screenheight()
    root.geometry('%dx%d+%d+%d' % (width*0.8, height*0.8, width*0.1, height*0.1))
     
    image_file = "10.gif"
    
    # use Tkinter's PhotoImage for .gif files
    image = tk.PhotoImage(file=image_file)
    canvas = tk.Canvas(root, height=height*0.8, width=width*0.8, bg="brown")
    canvas.create_image(width*0.8/2, height*0.8/2, image=image)
    canvas.pack()
     
    # show the splash screen for 1000 milliseconds then destroy
    root.after(1000, root.destroy)
    root.mainloop()
    Last edited by bvdet; Dec 21 '11, 03:50 PM. Reason: Please use code tags when posting code
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    I don't see what your problem is. If you adjust the size of the widget you must also adjust its location to keep it in the center of the screen.

    Comment

    • nazish zehra
      New Member
      • Dec 2011
      • 31

      #3
      but it's not working i cant understand it:(

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        nazish zehra,

        Your comment "but it's not working i cant understand it:(" does not help us understand your problem. What is not working about it? Are you receiving an error? If so, what is in the traceback? We cannot help you unless you help us.

        Comment

        • Glenton
          Recognized Expert Contributor
          • Nov 2008
          • 391

          #5
          Is it just the geometry you're struggling with?
          If m,n is width, length of image and
          M,N is width,length of canvas, then corner coordinates will be
          ((M-m)/2,(N-n)/2)

          Comment

          • nazish zehra
            New Member
            • Dec 2011
            • 31

            #6
            i can not adjust the coordinates.pro gram is not giving any error.i have problem with the coordinates and size of canvas only.

            image size is 150*150 and i cannot adjust the size and location of canvas
            Last edited by bvdet; Dec 23 '11, 04:26 PM. Reason: Should be one post

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              It is straightforward to determine the image size using PIL and calculate the splash screen position to center on the screen.
              Code:
              import Tkinter as tk
              from PIL import Image
              
              image_name = "10.gif"
              image_size = Image.open(image_name).size
               
              root = tk.Tk()
              # show no frame
              root.overrideredirect(True)
              width = root.winfo_screenwidth()
              height = root.winfo_screenheight()
              x = (width-image_size[0])/2.
              y = (height-image_size[1])/2.
              
              root.geometry('%dx%d+%d+%d' % (image_size[0], image_size[1], x, y))
              
              image = tk.PhotoImage(file=image_name)
              canvas = tk.Canvas(root, height=image_size[1], width=image_size[0], bg="white")
              canvas.create_image(0, 0, anchor="nw", image=image)
              canvas.pack()
              
              root.after(5000, root.destroy)
              root.mainloop()

              Comment

              • nazish zehra
                New Member
                • Dec 2011
                • 31

                #8
                thanks alot.It works.you hav solved my problem

                Comment

                • nazish zehra
                  New Member
                  • Dec 2011
                  • 31

                  #9
                  now splash problem is solved but when i call this splash program from another program, splash is shown but gui of the program i hav made is not shown.program runs without any error ,shows splash but doesnot show the interface i hav made in that program
                  Code:
                  import splashprog
                  splashprog.splash()#for splash screen
                  root = Tk()#my program interface
                  .....#code of my program
                  root.mainloop()

                  Comment

                  • dwblas
                    Recognized Expert Contributor
                    • May 2008
                    • 626

                    #10
                    You want to put the code in a function, and call the function from the other program. You may have to send "root" to the other program, and use a Toplevel in the first program, depending on what your second program does.
                    Code:
                    import splashprog
                     root = Tk()#my program interface
                     splashprog.splash(root)     #for splash screen
                     .....#code of my program
                     root.mainloop()
                    An example of the type that I use
                    Code:
                    ##------------- first program --------------------------
                    class splash():
                        def __init__(self, tk, root):
                            self.new_gui = tk.Toplevel(root)
                            lab = tk.Label(self.new_gui, text="test of calling another program")
                            lab.pack()
                    
                        def stop(self):
                            print  "stop called"
                            self.new_gui.destroy()
                    
                    ##------------- second program --------------------------
                    try:
                        import Tkinter as tk     ## Python 2.x
                    except ImportError:
                        import tkinter as tk     ## Python 3.x
                    
                    import first_prog
                    root=tk.Tk()
                    n=first_prog.splash(tk, root)
                    root.after(2000, n.stop)    ## destroy "splash" after 2 seconds
                    root.after(3000, root.quit) ## root remains 1 second longer
                    root.mainloop()

                    Comment

                    Working...