Posts: 93
Threads: 31
Joined: Nov 2017
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()
Posts: 12,028
Threads: 485
Joined: Sep 2016
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()
Posts: 93
Threads: 31
Joined: Nov 2017
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.
Posts: 5,151
Threads: 396
Joined: Sep 2016
Jun-26-2019, 10:39 PM
(This post was last modified: Jun-26-2019, 10:40 PM by metulburr.)
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:
Posts: 93
Threads: 31
Joined: Nov 2017
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
Posts: 93
Threads: 31
Joined: Nov 2017
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
Posts: 74
Threads: 0
Joined: Mar 2017
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 :-)
Posts: 93
Threads: 31
Joined: Nov 2017
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 .
Posts: 12,028
Threads: 485
Joined: Sep 2016
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
Posts: 74
Threads: 0
Joined: Mar 2017
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 :-)
|