I'm trying to size entry boxes using pixels, so I am having to create a frame to get the correct size. I am also using a class to create tkinter Entry boxes so that I can put placeholders into the boxes. These both work separately; yet, when put together, the entry box refuses to go onto the correct frame even though I have specified the frame.
import Tkinter as tk
from Tkinter import *
class GUI:
def __init__(self, root):
self.usernameFrame = Frame(root, height=50, width=300, bg="GREEN", bd=3)
self.usernameFrame.pack_propagate(0)
self.usernameFrame.pack()
self.usernameEntry = EntryPlaceholder(self.usernameFrame, "Username")
self.usernameEntry.pack()
class EntryPlaceholder(Entry, object):
def __init__(self, master=None, placeholder="placeholder"):
super(EntryPlaceholder, self).__init__()
self.placeholder = placeholder
self.bind("<FocusIn>", self.clearText)
self.bind("<FocusOut>", self.insertText)
self.insertText()
def insertText(self, *args):
if self.get() == "":
self.insert(0, self.placeholder)
def clearText(self, *args):
if self.get() == self.placeholder:
self.delete(0, END)
root = tk.Tk()
gui = GUI(root)
root.mainloop()
The widget just gets placed onto the root frame.
Consider this code:
def __init__(self, master=None, placeholder="placeholder"):
super(EntryPlaceholder, self).__init__()
You aren't passing master
on to the __init__
of EntryPlaceHolder
, so it's being put in the root window.
That code needs to be this:
def __init__(self, master=None, placeholder="placeholder"):
super(EntryPlaceholder, self).__init__(master)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.