简体   繁体   中英

Tkinter entry box not showing up

I am making a messaging program, and for some reason my Entry box titled "raw_input" is not showing up.

Code:

from tkinter import *
import datetime

def send_message(event):
    #this is a placeholder cuz without it it would give me an error
    q = "hi"
    #delete placeholder then your code here

# *** Initalize Window ***
root = Tk()
root.title("Secure MSG")
root.configure(background='grey')

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

root.geometry('%sx%s' % (int(screen_width/2), int(screen_height/2)))

# *** Connection Time Chat Box ***
chat_box = Listbox(root, width=screen_width, height=screen_height/2-450)
chat_box.insert(END, 'Connection: ' + str(datetime.datetime.now()))
chat_box.see('end')
chat_box.grid(row=0)

# *** Input Box ***
l1 = Label(root, text="Input:").grid(row=1, sticky=W)

raw_input = Entry(root)
raw_input.grid(row=1, sticky=E)
raw_input.focus()
raw_input.bind('<Return>', send_message)

# *** Run Mainloop ***
root.mainloop()

Any help will be appreciated. Thanks!

The cause of your problem is that the units of width and height for the listbox are in characters, so you are setting creating a huuuuuge listbox, which covers everything else under it. Further, by specifying the root geometry, you are setting a static window size which is not large enough to contain all of the widgets you've placed in it. It's typically better to just let the geometry manager handle fitting things.

One way would be to use columnspan to help a little bit, something like

chatbox.grid(row=0, column=0, columnspan=2, sticky=NSEW)
...
l1 = Label(root, text="Input:")
l1.grid(row=0, column=0, sticky=EW)
grid_column
...
raw_input.grid(row=1, column=1, sticky=EW)
root.columnconfigure(0, weight=0)
root.columnconfigure(1, weight=1)
root.rowconfigure(0, weight=1)

(A point of note: you are assigning the return of grid() to l1 in your code. This is None, and will likely cause you problems, you need to assign the widget first.)

By changing your sticky to EW, the raw_input will expand to fill its cell, as will the label. By setting weights for the columns, they will adjust with the window size if the user changes it.

You could also use pack with another frame for the entry widget and label.It's a bit simpler to implement. Here is your code adjusted to use it.

from tkinter import *
import datetime

def send_message(event):
    #this is a placeholder cuz without it it would give me an error
    q = "hi"
    #delete placeholder then your code here

# *** Initalize Window ***
root = Tk()
root.title("Secure MSG")
root.configure(background='grey')

# *** Connection Time Chat Box ***
chat_box = Listbox(root)
chat_box.insert(END, 'Connection: ' + str(datetime.datetime.now()))
chat_box.see('end')
chat_box.pack(side=TOP, fill=BOTH, expand=True)
# *** Input Box ***

tempFrame = Frame(root)
tempFrame.pack(side=TOP, fill=X)
l1 = Label(tempFrame, text="Input:", width=5)
l1.pack(fill=X)

raw_input = Entry(tempFrame)
raw_input.pack(fill=X, expand=True)
raw_input.focus()
raw_input.bind('<Return>', send_message)

# *** Run Mainloop ***
root.mainloop()

This will create a window which adjusts as the user resizes it.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM