1,105,585 Community Members

location of textbox or label in window

Member Avatar
DanWebb3148
Newbie Poster
9 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I know how to create a window of a given size. How do I control the placement of items like labels and text boxes in that window?

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

With the Tkinter GUI toolkit there a several ways to layout the position of the widgets. Here is an example of an absolute place layout ...

# set position and size of a Tkinter window
# also set the position of any widgets using place(x, y) layout

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

# create the root window
root = tk.Tk()
# optionally give it a title
root.title("My Title")

# set the root window's height, width and x,y position
# x and y are the coordinates of the upper left corner
w = 300
h = 200
x = 50
y = 100
# use width x height + x_offset + y_offset (no spaces!)
root.geometry("%dx%d+%d+%d" % (w, h, x, y))

# use a colorful frame
frame = tk.Frame(root, bg='green')
frame.pack(fill='both', expand='yes')

# position a label on the frame using place(x, y)
# place(x=0, y=0) would be the upper left frame corner
label = tk.Label(frame, text="Hello Python Programmer!")
label.place(x=20, y=30)

# put the button below the label, change y coordinate
button = tk.Button(frame, text="Press me", bg='yellow')
button.place(x=20, y=60)

root.mainloop()
Member Avatar
DanWebb3148
Newbie Poster
9 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

This looks like it will be what I need. Let me try it out.

+

With the Tkinter GUI toolkit there a several ways to layout the position of the widgets. Here is an example of an absolute place layout ...

# set position and size of a Tkinter window
# also set the position of any widgets using place(x, y) layout

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

# create the root window
root = tk.Tk()
# optionally give it a title
root.title("My Title")

# set the root window's height, width and x,y position
# x and y are the coordinates of the upper left corner
w = 300
h = 200
x = 50
y = 100
# use width x height + x_offset + y_offset (no spaces!)
root.geometry("%dx%d+%d+%d" % (w, h, x, y))

# use a colorful frame
frame = tk.Frame(root, bg='green')
frame.pack(fill='both', expand='yes')

# position a label on the frame using place(x, y)
# place(x=0, y=0) would be the upper left frame corner
label = tk.Label(frame, text="Hello Python Programmer!")
label.place(x=20, y=30)

# put the button below the label, change y coordinate
button = tk.Button(frame, text="Press me", bg='yellow')
button.place(x=20, y=60)

root.mainloop()
Member Avatar
DanWebb3148
Newbie Poster
9 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Sorry, I can't get your code to work in my program. I am trying to get an about box to look the way I want to. Here is my code. Can you move the two labels left and right of center. I would like to eventually have a icon picture to the right of the two labels and a list box below.

# Lemniscate Curve
from Tkinter import *

root = Tk()

# create the root window
root.title('Lemniscate Curve')
canvas = Canvas(root, width =815, height=560)

def showAbout():
    t1 = Toplevel(root)
    t1.geometry("430x300")
   
    t1.title('About Lemniscate')
    Label(t1, text='Draw Lemniscate Curve', font=("Arial", 14)).pack(pady=30)  # Can you get ride of the 'pack'?
    Label(t1, text='Programer: Dan Webb', font=("Arial", 14)).pack()
    
# create a menu
menu = Menu(root)
root.config(menu=menu)

filemenu = Menu(menu)
#menu.add_cascade(label="About", menu=filemenu)
menu.add_command(label="About", command=showAbout)

#canvas.create_bitmap(355, 53, bitmap='questhead')
canvas.create_line(50, 280, 750, 280, width=2)  # fill='purple'
canvas.create_line(400, 50, 400, 510, width=2)

def CalulateY(x):
    a = 300.0
    yh = 120.0
    yl = 0.0
    y = 125.0
    for i in range(0,20):
        dhl = yh - yl
        if (dhl < 0.1):
            return y
        y = (yh + yl) / 2.0

        x2 = x*x
        y2 = y*y
        t = x2 + y2
        dif = t * t - a * a * (x2 - y2)

        if (dif > 0):
            yh = y;
        else:
            yl = y;

XplotS = 400
YplotS = 280
XplotS2 = 400
YplotS2 = 280
XplotS3 = 400
YplotS3 = 280
XplotS4 = 400
YplotS4 = 280
for x in range(1,301):
    y = CalulateY(x)
    XplotE = 400 + x
    YplotE = 280 + y
    canvas.create_line(XplotS, YplotS, XplotE, YplotE, width=2, fill='purple')
    XplotS = XplotE
    YplotS = YplotE

    XplotE2 = 400 + x
    YplotE2 = 280 - y
    canvas.create_line(XplotS2, YplotS2, XplotE2, YplotE2, width=2, fill='purple')
    XplotS2 = XplotE2
    YplotS2 = YplotE2

    XplotE3 = 400 - x
    YplotE3 = 280 + y
    canvas.create_line(XplotS3, YplotS3, XplotE3, YplotE3, width=2, fill='purple')
    XplotS3 = XplotE3
    YplotS3 = YplotE3

    XplotE4 = 400 - x
    YplotE4 = 280 - y
    canvas.create_line(XplotS4, YplotS4, XplotE4, YplotE4, width=2, fill='purple')
    XplotS4 = XplotE4
    YplotS4 = YplotE4    
     
canvas.pack()
root.mainloop()

With the Tkinter GUI toolkit there a several ways to layout the position of the widgets. Here is an example of an absolute place layout ...

# set position and size of a Tkinter window
# also set the position of any widgets using place(x, y) layout

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

# create the root window
root = tk.Tk()
# optionally give it a title
root.title("My Title")

# set the root window's height, width and x,y position
# x and y are the coordinates of the upper left corner
w = 300
h = 200
x = 50
y = 100
# use width x height + x_offset + y_offset (no spaces!)
root.geometry("%dx%d+%d+%d" % (w, h, x, y))

# use a colorful frame
frame = tk.Frame(root, bg='green')
frame.pack(fill='both', expand='yes')

# position a label on the frame using place(x, y)
# place(x=0, y=0) would be the upper left frame corner
label = tk.Label(frame, text="Hello Python Programmer!")
label.place(x=20, y=30)

# put the button below the label, change y coordinate
button = tk.Button(frame, text="Press me", bg='yellow')
button.place(x=20, y=60)

root.mainloop()
Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

Experiment with side and expand parameters to pack and use help frames! You must assign frame first and pack it separately to use it for other widgets.

def showAbout():
    t1 = Toplevel(root)
    t1.geometry("430x300")
   
    t1.title('About Lemniscate')
    f = Frame(t1)
    f.pack(side=LEFT, expand=False)
    Label(f, text='Draw Lemniscate Curve', font=("Arial", 14)).pack(pady=30, side=TOP)  # Can you get ride of the 'pack'?
    Label(f, text='Programer: Dan Webb', font=("Arial", 14)).pack(side=TOP)
Member Avatar
DanWebb3148
Newbie Poster
9 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Close, but I was hoping to place the Labels with something like 'label.place(x=5, y=30)'

Experiment with side and expand parameters to pack and use help frames! You must assign frame first and pack it separately to use it for other widgets.

def showAbout():
    t1 = Toplevel(root)
    t1.geometry("430x300")
   
    t1.title('About Lemniscate')
    f = Frame(t1)
    f.pack(side=LEFT, expand=False)
    Label(f, text='Draw Lemniscate Curve', font=("Arial", 14)).pack(pady=30, side=TOP)  # Can you get ride of the 'pack'?
    Label(f, text='Programer: Dan Webb', font=("Arial", 14)).pack(side=TOP)
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: