Posts: 5
Threads: 1
Joined: May 2018
Hello to all.
I created a frame and two buttons, I assigned two functions to the buttons, one closes the window, the other displays the / etc / fstab file in the shell.
I would like to be able to view the output of the second function within the frame.
from tkinter import *
import os
#window
mywin=Tk()
#function about to close window
def close_window (root):
root.destroy()
#function about to read fstab
def fstab():
os.system("cat /etc/fstab")
#size window
mywin.geometry("600x400+600+100")
mywin.title('WINDOW')
#text in window
mytext=Label(text='View', fg='white', bg='black', font=('Helvetica',30)).pack()
#button GO!
gobutton=Button(text='GO!', command = fstab).pack()
#frame for my output
myframe=Frame(width=300, height=300).pack()
#button Exit
exitbutton=Button (mywin, text="Exit", command = lambda: close_window(mywin)).pack()
#go mainloop
mywin.mainloop()
Posts: 536
Threads: 0
Joined: Feb 2018
May-25-2018, 07:57 PM
(This post was last modified: May-25-2018, 07:57 PM by woooee.)
Note that you have to have read permission on fstab (Code not tested so post back if there are errors that you can not fix) Pmw has a ScrolledFrame if you want to install it https://pypi.org/project/Pmw/ def fstab():
records=open("/etc/fstab", "r").readlines()
listbox = Listbox(mywin, height=6, width=20, font=('Fixed', 14),
foreground='blue' )
scrolly = Scrollbar(mywin, command=listbox.yview )
listbox.configure(yscrollcommand=scrolly.set)
for item in records:
listbox.insert("end", item)
scrolly.pack(side="right", fill="y")
Posts: 5
Threads: 1
Joined: May 2018
(May-25-2018, 07:57 PM)woooee Wrote: Note that you have to have read permission on fstab (Code not tested so post back if there are errors that you can not fix) Pmw has a ScrolledFrame if you want to install it https://pypi.org/project/Pmw/
Thank you so much for your answer. I tried to use your code by editing my file this way
from tkinter import *
#window
mywin=Tk()
#function about to close window
def close_window (root):
root.destroy()
#function about to read fstab
def fstab():
records=open("/etc/fstab", "r").readlines()
listbox = Listbox(mywin, height=6, width=20, font=('Fixed', 14),
foreground='blue' )
scrolly = Scrollbar(mywin, command=listbox.yview )
listbox.configure(yscrollcommand=scrolly.set)
for item in records:
listbox.insert("end", item)
scrolly.pack(side="right", fill="y")
#size window
mywin.geometry("600x400+600+100")
mywin.title('WINDOW')
#text in window
mytext=Label(text='View', fg='white', bg='black', font=('Helvetica',30)).pack()
#button GO!
gobutton=Button(text='GO!', command = fstab).pack()
#button Exit
exitbutton=Button (mywin, text="Exit", command = lambda: close_window(mywin)).pack()
#go mainloop
mywin.mainloop() as you suggested, I installed pwm.
on the screen appear the vertical scroll bar, but without, unfortunately, return, no code in the new output.
Posts: 536
Threads: 0
Joined: Feb 2018
You have to pack the listbox for it to show listbox.pack(side="left") It is best if you learn to figure out how to do this for yourself.
Posts: 5
Threads: 1
Joined: May 2018
thank you so much! I studied the Tkinter Listbox functions, now I understand !!
now I would like to take a look at Pwm which I installed, but not used.
what I would like to know now , if I can take advantage of your availability, is how to give an executable command, for example /usr/bin/lsblk or /usr/bin/sensors.
Posts: 5
Threads: 1
Joined: May 2018
I tried this way:
import subprocess
def lsblk():
records = subprocess.Popen('/usr/bin/lsblk', shell=True).readlines() in this way, I display the output in bash.
I'm returning this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.6/tkinter/__init__.py", line 1702, in __call__
return self.func(*args)
File "os.py", line 20, in lsblk
records =subprocess.Popen('/usr/bin/lsblk').readlines()
AttributeError: 'Popen' object has no attribute 'readlines' my version python 3.6 and 2.7
I use as os arch linux
Posts: 5
Threads: 1
Joined: May 2018
finally I succeeded!
I have directed the output of the process in an external file and then open it, making sure I have read permission, with the usual built-in "open" function
then I place the code if this to someone can to be help:
import os
def lsblk():
os.system('lsblk >outfile')
records=open('outfile', 'r').readlines()
|