1

I am trying to make a window that would take an input through an entry and that would be either a web address or ip address and i would use a loop to update the text of a label to show the current ping every second. But I'm stuck at the very beginning because my entry would not appear on my window. Here is my code:

import tkinter as tk
from tkinter import *


window = tk.Tk()
window.title("Server Status")
window.geometry('400x600')
window.resizable(0,0)

canvas = tk.Canvas(window,height=600,width=1000,bg='#263D42')
canvas.pack()


txtf=tk.Entry(window, width=10)
txtf.pack()



window.mainloop()

Where am I going wrong? I have tried it with several changes but still cant get it to appear there. Any help would be appreciated.

Thanks.

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
Jyotirmay
  • 513
  • 6
  • 22
  • 1
    I think the canvas is hiding the entry widget, you can see it by saying `txtf.pack(before=canvas)`, this will pack entry first, then the canvas. Its always there, its just that you are not allowing the window to be resized and fixing a size to it. So get rid of the `window.geometry()` – Delrius Euphoria Oct 27 '20 at 04:21
  • 1
    @CoolCloud Thank you it worked! But can you help me with one more thing? I want the entry to be embedded on the canvas but packing it before makes a separate partition for that like it shifts the canvas depending on the side of packing of the entry. I want the entry to be on top of the canvas. – Jyotirmay Oct 27 '20 at 04:27
  • 1
    Ok so I went through python's documentation and found out that i can use the canvas.create_window() method to embed it. Btw thanks @CoolCloud. – Jyotirmay Oct 27 '20 at 04:32
  • Yep, something like this `canvas.create_window(100,20,window=txtf)` – Delrius Euphoria Oct 27 '20 at 04:34

1 Answers1

2

Your entry is below the canvas, but because (1) your window geometry specifies a smaller size than that requested for the canvas, and (2) you set it to be non resizable, you can never access it.

Choose how to resolve this conflict; the example below sets the size of the canvas, and lets the window resize to enclose all its widgets.

import tkinter as tk


window = tk.Tk()
window.title("Server Status")

canvas = tk.Canvas(window, height=600, width=1000, bg='#263D42')
canvas.pack()

txtf = tk.Entry(window, width=10)
txtf.pack()

window.mainloop()
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80