Hey Guys.

I've been given a Major for school where I have to create a Mathematics program in Python.
The problem I am having is that If i try to set up a canvas with a background image, the buttons will not show any more!?

the second problem I am having is that say i set a Label to row = 1, column = 1 .. it seems to want to sit in the middle of the screen.

I cant post code at the moment. I will try to later.

I hope you can help :)

Recommended Answers

All 11 Replies

Hey Guys.

I've been given a Major for school where I have to create a Mathematics program in Python.
The problem I am having is that If i try to set up a canvas with a background image, the buttons will not show any more!?

the second problem I am having is that say i set a Label to row = 1, column = 1 .. it seems to want to sit in the middle of the screen.

I cant post code at the moment. I will try to later.

I hope you can help :)

You need to be more specific.

Firstly, what platform are you using? What graphical toolkit? Are you trying to make a calculator? Can you post your code now?

I will have to code in the morning, im sorry for such a fuss!

Im using XP, Tkinter..

Basically, the program has to generate 10 questions of the users choosing - Addition, subtraction etc. The questions will vary in difficulty according to the difficulty they choose aswell.
after that the results are gathered and a certificate is presented.

I hope this is enough as i said. First thing tommorrow. Promise, sorry!

For some odd reason that question has come up and has been solved several times in the past. One quick search will certainly help you.

Or you could just read up on the behavior of the canvas widget. Honestly doesn't really sound like you need the canvas.

Labels and Text widgets should do it.

I have read up on that stuff. But I can't find any way to get a background image on the program then.. I'm new to this.

I appreciate the help. I have the code too.

### Program created By Alex 
###  14 / July / 2009

from Tkinter import *
import math


### Establishes Global variables




class App(Frame):
    def __init__(self,master):
        """ Initializing the frame. """
        Frame.__init__(self,master)
        self.grid()
        self.SplashPage()
        
    def SplashPage(self):
        ####### Sets Page Title
        root.title("Welcome to Aus math")
        ########   Creates the splash page for the program
        #canvas = Canvas(bg = 'yellow',width=500, height=500)
        #canvas.grid(row = 0, column = 0, sticky = W)
      
        genericButton=Button(fg="blue", bg="red", text="GENERATE", command=self.entername)

        c = Canvas(width = 800, height = 600, bg = "yellow")
        c.grid(row = 0, column = 0, sticky = W)
        c.create_window(10, 10, window=genericButton)

        #######   Create continue Button
        self.image2 = PhotoImage(file="bg.gif")
        self.cont_bttn = Button(self, image = self.image2 ,command = self.entername)
        self.cont_bttn.grid(row = 100, column = 50, sticky = W)
        self.cont_bttn1 = self.image2
        
      
    def entername(self):

        del(self.image2)
        self.cont_bttn.destroy()
        root.title("Enter your user name - Aus Math")
        self.name_lbl = Label(self, text = "Enter Name:  ")
        self.name_lbl.grid(row = 1, column = 0, sticky = W)
              
        self.name_ent = Entry(self)
        self.name_ent.grid(row = 1, column = 1, sticky = W)


        user_name = self.name_ent.get()


        self.entername_button =  Button(self, bg = "green", text = "Enter & Continue", command = self.ChooseDifficulty)
        self.entername_button.grid(row = 2, column = 1, sticky = W)



    def ChooseDifficulty(self):

        self.entername_button.destroy()
        self.name_lbl.destroy()
        self.name_ent.destroy()
        
        
        menubar = Menu(root, background = 'red')
        root.config(menu=menubar)
        menubar.add_command(label="About the Program      ", command=self.aboutprogram)
        menubar.add_separator()
        menubar.add_command(label="About the Author      ", command=self.aboutauthor)
        menubar.add_separator()
        menubar.add_command(label="Help", command=self.proghelp)
    
        
        
        self.title_lbl = Label(self,text = "Select your choice")
        self.title_lbl.grid( row = 1, column = 1, sticky = W)
        


    #### About the program ###
    def aboutprogram(self):
        author = "Alex"
        self.intro_to_prog_lbl = Label(self, text = "This program was designed for use by primary school students looking to test their knowledge in the area of Mathematics. \n It was created in the Python Programming language using Tkinter graphics pack.")
        self.intro_to_prog_lbl.grid(row = 1, column = 1, sticky = W)
    

    #### About the Author  ###
    def aboutauthor(self):
        ## Destroys previous
        
        self.cont_bttn.destroy()
        self.intro_to_prog_lbl.destroy()


        
        author = "Alex"
        self.aboutAuth = Label(self, text = "This program was design and create by Alex  for a Software Design and Development task.")
    
        self.aboutAuth.grid(row = 1, column = 1, sticky = W)



    def proghelp(self):
        self.entername_button.destroy()
        self.name_lbl.destroy()
        self.name_ent.destroy()
        self.aboutAuth.destroy()
        self.intro_to_prog_lbl.destroy()
        self.title_lbl.destroy()


        self.help_lbl = Label(self, text = """
                                                How to use the program:

                                                    Make a selection on Easy, Medium or Hard in either the addition column or the subtraction column.
                                                    answer the questions and click submit.

                                                Navigating the program:

                                                    Using the Navigational menu at the top of the screen, you can navigate around the program.

                                                    """)
        self.help_lbl.grid(row = 1, column = 1, sticky = W)
        
        
root = Tk()
root.geometry('800x600')
app = App(root)

root.mainloop()

I know its messy!! thanks again!

Ewwww. Half your variables aren't even defined dude. You can't say:

def SplashScreen(self):
          root.title("My Title")

What is root? When do you define root?

For background images, google canvas Tkinter. You will also need PIL, unless your background image is a bmp.

That root.title bit works though? It changes the name each page.

Ive looked into the canvas thing and i cant get buttons ontop of the background
can anyone help?

Ewwww. Half your variables aren't even defined dude. You can't say:

def SplashScreen(self):
          root.title("My Title")

What is root? When do you define root?

If you scrolled to the bottom of the code, after the class definition, you'd see that he defined root as Tk() :P

If you scrolled to the bottom of the code, after the class definition, you'd see that he defined root as Tk() :P

Right, it's defined outside of the class, it's much easier and cleaner to pass root to the class:

class App:
     def __init__(self,root):
          self.root = root

He's doing a similar thing, (passing master to the class) but he's not making it part of the class, or ever referencing it again. Not a good idea.

As for your canvas issue, I'm not a canvas wiz myself, but I think in general, you have to add widgets to your canvas (as opposed to adding them to master or root), and from looking at your code, I can see that you grid your canvas before adding anything at all to it.

Also, if all your canvas is just supposed be a solid background color, I would suggest using a Frame. Same rule applies though, add your buttons and such to the frame.

Here is an example for how to use a Tkinter canvas for a background image ...

# use Tkinter canvas create_image for background
# put some buttons on it for testing

try:
    # for Python2
    import Tkinter as tk
except ImportError:
    # for Python3
    import tkinter as tk
 
root = tk.Tk()
root.title('canvas background image')

# pick a .gif image file you have in the working 
# directory or give the full path
bg_image = tk.PhotoImage(file="LAKE.GIF")
w = bg_image.width()
h = bg_image.height()
 
# size the window so the image will fill it
root.geometry("%dx%d+90+80" % (w, h))

cv = tk.Canvas(width=w, height=h)
cv.pack(side='top', fill='both', expand='yes')
cv.create_image(0, 0, image=bg_image, anchor='nw')
 
# add some button widgets
btn1 = tk.Button(cv, text="Click")
btn1.pack(side='left', padx=10, pady=5, anchor='sw')

btn2 = tk.Button(cv, text="Quit")
btn2.pack(side='left', padx=10, pady=5, anchor='sw')

root.mainloop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.