Python Forum
thinker button gui place next to each other
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
thinker button gui place next to each other
#1
HI ,
do anyone knows how to place clear button next to ping button.
Right now it display ping and clear is not place together.
if i put pack(side"left"), it will look really strange.
Is there any method to let ping and clear to be next to each other. Which method should i used?
import tkinter as tk
import subprocess
def clear_text():
    result.destroy()

def ping():
    cmd = ["ping", entry.get(), "-c", "2"]
    output = subprocess.check_output(cmd)
    #output = subprocess.check_output("ping {} -c 2".format(entry.get()), shell=True)
    #print under console
    #print('>', output)
    #with open('test.txt', 'w') as f:
    #cls    print(output, file=f)
    

    # put result in label
    result['text'] = output.decode('utf-8')

my_gui = tk.Tk()
entry = tk.StringVar()

my_gui.geometry('300x300')
my_gui.title("Get output inside GUI") 

tk.Label(my_gui, text="Enter target IP or host as required.",bg = 'red').pack(side='top') 
tk.Entry(my_gui, textvariable=entry).pack()
tk.Button(my_gui,text="Ping Test", command=ping).pack() 
tk.Button(my_gui,text="Clear", command=clear_text).pack() 

# label for ping result
result = tk.Label(my_gui)
result.pack(side='bottom')



my_gui.mainloop()
Reply
#2
Pack is a stationary geometry, and won't allow moving windows or widgets, I never use it, however, you can specify the x, y coordinates
or try using border.
like:
tk.Button(my_gui,text="Ping Test", bd=2, command=ping).pack() 
tk.Button(my_gui,text="Clear", bd=2, command=clear_text).pack() 
Reply
#3
i try with this
tk.Button(my_gui,text="Ping Test", bd=2, command=ping).pack() 
tk.Button(my_gui,text="Clear", bd=2, command=clear_text).pack() 
seem like the same.

Does pack this method allow button to place next to each other.
i saw grid is able to do like it on some tutorial.
Reply
#4
Quote:Right now it display ping and clear is not place together.
if i put pack(side"left"), it will look really strange.

If you just try to pack it to side left then it is going to attempt to center it on the Y axis making it far down. You have to anchor it the north.

try using anchor to northwest
tk.Button(my_gui,text="Ping Test", bd=2, command=ping).pack(side='left', anchor='nw') 
tk.Button(my_gui,text="Clear", bd=2, command=clear_text).pack(side='left', anchor='nw') 
Or a little more uniformed
tk.Button(my_gui,text="Ping Test", bd=2, command=ping).pack(side='left', anchor='nw', padx=25) 
tk.Button(my_gui,text="Clear", bd=2, command=clear_text).pack(side='right', anchor='ne', padx=25) 
or even more uniformed
tk.Button(my_gui,text="Ping Test", bd=2, command=ping).pack(side='left', anchor='n', fill='x',expand='yes') 
tk.Button(my_gui,text="Clear", bd=2, command=clear_text).pack(side='right', anchor='n', fill='x',expand='yes') 
 
depending on your tastes.
Recommended Tutorials:
Reply
#5
this work
tk.Button(my_gui,text="Ping Test", bd=2, command=ping).pack(side='left', anchor='nw') 
tk.Button(my_gui,text="Clear", bd=2, command=clear_text).pack(side='left', anchor='nw') 
but how to place in center or move space
Reply
#6
HI again,
i modify something, do anyone knows how to let the button to place next to each other.
right now the there's label in top and bottom. The button is replace middle of label.
is there any possible to replace "ping test", and "clear' button next to each other.

i try with side=left, but it's not what i wants.
any other advice.



import tkinter as tk
import subprocess
def clear_text():
    result.destroy()
 
def ping():
    cmd = ["ping", entry.get(), "-n", "2"]
    output = subprocess.check_output(cmd)
    #print (output)
    #output = subprocess.check_output("ping {} -c 2".format(entry.get()), shell=True)
    #print under console
    #print('>', output)
    #with open('test.txt', 'w') as f:
    #cls    print(output, file=f)
     
 
    # put result in label
    #result['text'] = output.decode('utf-8')
    result['text'] = output.decode('big5')
 
my_gui = tk.Tk()
entry = tk.StringVar()
 
my_gui.geometry('300x300')
my_gui.title("Get output inside GUI") 
 
tk.Label(my_gui, text="Enter target IP or host as required.",bg = 'red').pack(side='top') 
tk.Entry(my_gui, textvariable=entry, bg = 'yellow').pack()
#tk.Button(my_gui,text="Ping Test", command=ping).pack() 
#tk.Button(my_gui,text="Clear", command=clear_text).pack(ipadx=10) 
tk.Button(my_gui,text="Ping Test", bd=2, command=ping).pack(side='top') 
tk.Button(my_gui,text="Clear", bd=2, command=clear_text).pack(side='top') 


# label for ping result
result = tk.Label(my_gui,bg='blue', height="20", width="50")
result.pack()
 
 
 
my_gui.mainloop()
thanks
Reply
#7
Hi jacklee26

Just put your Buttons in a Frame (button_frame) and use side='left' for the Buttons placement:
button_frame = tk.Frame(my_gui)
button_frame.pack()
tk.Button(button_frame,text="Ping Test", bd=2, command=ping).pack(side='left') 
tk.Button(button_frame,text="Clear", bd=2, command=clear_text).pack(side='left') 
wuf :-)
Reply
#8
HI wurf,
it works.
i'm just curious after adding this line, it work. How come? one is tk.tk, another one is tk.frame.

button_frame = tk.Frame(my_gui)
button_frame.pack()
originally without adding it will be strange.

thanks for your help, appreciated .
Reply
#9
It's tk.Tk and tk.Frame
each (Tk, Frame) are methods of the tk class ('.') means package.method
so tk.Tk : Tk method of class tk
Reply
#10
Hi jacklee26

When using the pack Layout Manager frames are mainly used as invisible containers to group individual widgets in a group. So you can handle the widget group when placing it with the help of a frame like a single widget.

For a try, i have modified your script with several such container frames:
import tkinter as tk
import subprocess
def clear_text():
    result.destroy()
  
def ping():
    cmd = ["ping", entry.get(), "-n", "2"]
    output = subprocess.check_output(cmd)
    #print (output)
    #output = subprocess.check_output("ping {} -c 2".format(entry.get()), shell=True)
    #print under console
    #print('>', output)
    #with open('test.txt', 'w') as f:
    #cls    print(output, file=f)
      
  
    # put result in label
    #result['text'] = output.decode('utf-8')
    result['text'] = output.decode('big5')

# This is the main window  
my_gui = tk.Tk()
entry = tk.StringVar()
  
my_gui.geometry('300x300')
my_gui.title("Get output inside GUI") 

# This is the main frame in the main window.
# With the pack properties padx & pady the main_frame gets an 4 pixel border and
# the expand causes the frame to be placed at the center of the screen by
# expanding the main window of the gui to full screen size.
main_frame = tk.Frame(my_gui)
main_frame.pack(padx=4, pady=4, expand=True)

# This frame is used to contain mainly the entry related widgets and
# is placed in the main_frame.
entry_frame = tk.Frame(main_frame)
entry_frame.pack(pady=4)

tk.Label(entry_frame, text="Enter target IP or host as required.",bg = 'red'
    ).pack(side='top') 
tk.Entry(entry_frame, textvariable=entry, bg = 'yellow').pack()

# This frame is used to contain mainly the control related widgets like buttons and
# is placed also in the main_frame
button_frame = tk.Frame(main_frame)
button_frame.pack(pady=4)

tk.Button(button_frame,text="Ping Test", bd=2, command=ping).pack(side='left') 
tk.Button(button_frame,text="Clear", bd=2, command=clear_text).pack(side='left') 

# label for ping result
# This label widget is also placed in the main_frame
result = tk.Label(main_frame, bg='blue', height="20", width="50")
result.pack()
  
my_gui.mainloop()
Hope you understand what i am trying to explain?
wuf :-)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Place QT Window in the middle AlphaInc 10 2,041 Aug-03-2023, 05:40 PM
Last Post: Axel_Erfurt
  [Tkinter] how to make label or button not visible with the place method? nowayj63 2 2,636 Jan-03-2023, 06:29 PM
Last Post: Yoriz
  Label.Place did not work? ATARI_LIVE 15 5,056 Sep-18-2020, 04:22 PM
Last Post: ATARI_LIVE
  [Tkinter] How to place scroll bar correctly scratchmyhead 1 3,896 May-18-2020, 04:17 PM
Last Post: scratchmyhead
  How to use place holders in tkinter sqlite scratchmyhead 1 1,782 May-12-2020, 06:13 PM
Last Post: Larz60+
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,949 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020