Home > Enterprise >  I need to centralize the L1-'f(x)=' Label and the E-Entry next to each other but I need th
I need to centralize the L1-'f(x)=' Label and the E-Entry next to each other but I need th

Time:12-11

I need to place the f(x)= and the entry widget next to each other but on the center of the screen.

I tried pack, I also tried the .place and it works but when it's opened on another computer with a different screen size it looks misplaced, so it doesn't work with .place.

from tkinter import*
from matplotlib import pyplot as plt
from numpy import*

t = Tk()
def odust():
    t.destroy()
    return
def prikaz():
    x = linspace(-20, 20)
    plt.ylim(-20,20)
    plt.plot(x, eval(str(E.get())), linewidth=1, color="black")
    yaxis=range(-20,20)
    xaxis=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    plt.plot(xaxis,yaxis,linewidth=0.5,color="black")
    plt.plot(x,x*0,linewidth=0.5,color="black")
    plt.show()
    return
t.attributes('-fullscreen', True)
t.config(bg="white")
xbutton = Button(t, text=" X ", bg="red", fg="white", font=("Goudy stout", 16), command=lambda:[odust()])
xbutton.pack(side="top", anchor="ne")
L=Label(t, text="Upišite u sivo polje funkciju grafa\nkoji želite prikazati.\n(Nap. razlomke pišite kao (a b)/c)\n(Kod umnoska pišite '*', npr 3*x)",bg="white", fg="black",font=("Goudy Stout", 18))
L. pack(side="top", pady=20)
natrag=Button(t, text="Natrag", bg="black",fg="white", font=("Goudy stout", 16) ,command=lambda:[odust()])
natrag.pack(side="bottom", pady=20)
prikazgraf=Button(t, text="Prikaži graf", fg="white", bg="black",font=("Goudy stout", 16),command=lambda:[prikaz()])
prikazgraf.pack(side="bottom",pady=20)
L1 = Label(t, text="f(x)=", bg="white", fg="black", font=("Goudy Stout", 12))
L1.pack(side="left", anchor="center", pady=20)
E=Entry(t, bg="gray", fg="black",font=("Goudy Stout", 12))
E.pack(side="left", anchor="center", pady=20)
t.mainloop()

CodePudding user response:

It is easier to put the label and entry box at the center of screen together by putting them in a frame.

...
frame = Frame(t, bg="white")
frame.place(relx=0.5, rely=0.5, anchor="c") # put frame at the center of screen
# create label and entry inside the frame
L1 = Label(frame, text="f(x)=", bg="white", fg="black", font=("Goudy Stout", 12))
L1.pack(side="left", anchor="center", pady=20)
E=Entry(frame, bg="gray", fg="black",font=("Goudy Stout", 12))
E.pack(side="left", anchor="center", pady=20)
...

enter image description here

  • Related