sar kite
sar kite

Reputation: 92

tkinter gets extra blank gui and no mouse event over image

I have a coding which will output the coordinates(row and column) in the gui present anywhwere when double Clicked.

Problem:

1.I wanted an background image,if i wanted to know the coordinates over the background image ,even after double click i am not getting the result.

2.it produces 2 gui,where one gives the original gui and another is a small blank gui,which i havenot asked for.I need only the first tkinter window not the second

from Tkinter import *
import tkMessageBox
import Tkinter
import Tkinter as tki
import tkFileDialog as th1

class App(object):

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

    # create a Frame for the Text and Scrollbar
        txt_frm = tki.Frame(self.root, width=900, height=900)
        txt_frm.pack(fill="both", expand=True)
        # ensure a consistent GUI size
        txt_frm.grid_propagate(False)

    # create first Text label, widget and scrollbar
        self.lbl1 = tki.Label(txt_frm, text="Type")
        self.lbl1.grid(row=0,column=0,padx=2,pady=2)

        self.txt1 = tki.Text(txt_frm, borderwidth=3, relief="sunken", height=4,width=55)
        self.txt1.config(font=("consolas", 12), undo=True, wrap='word')
        self.txt1.grid(row=25, column=7, sticky="nsew", padx=2, pady=2)

        scrollb1 = tki.Scrollbar(txt_frm, command=self.txt1.yview)
        scrollb1.grid(row=25, column=7, sticky='nsew')
        self.txt1['yscrollcommand'] = scrollb1.set
        button = tki.Button(txt_frm,text="Click After Text", command = self.retrieve_input)

        button.grid(column=7,row=27)
        button1 = tki.Button(txt_frm,text="Clear", command = self.clearBox)
        button1.grid(column=7,row=28)

        self.image = Tkinter.PhotoImage(file='linux.gif') 

        image1 = Tkinter.Label(txt_frm, image=self.image).grid() 
        def clicked(event):
            n=100
            print "The user clicked at coordinates", event.x/n, event.y/n
            a='Row=%s,Column=%s'%(event.x/n, event.y/n)

        txt_frm.bind("<Double-Button-1>", clicked)


    def retrieve_input(self):
        input1 = self.txt1.get("0.0",'end-1c')
        with open('textme.txt','a+') as f:
            f.write(input1+'\n')
        f.close()
    def clearBox(self):
        self.txt1.delete('1.0', 'end')#<-0.0/1.0

def file_save():
    f = th1.asksaveasfile(mode='w', defaultextension=".txt")
    filename = f.name

    return filename
root = tki.Tk()
menubar=Menu(root)
root.configure(menu=menubar)

filemenu=Menu(menubar,tearoff=0)
menubar.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Save", command=file_save)        
app = App(root)
root.mainloop()

Please mention the mistakes over my coding.

Upvotes: 0

Views: 198

Answers (1)

TidB
TidB

Reputation: 1759

The problem is that you're binding the double click event to txt_frm (which is a bad name btw, use the full name instead) instead of root, as you want your method clicked to be called whenever a double click is performed. Just change this one line to

self.root.bind("<Double-Button-1>", clicked)

If you want to use the image as the background, see this question

Upvotes: 2

Related Questions