Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
debian/ubuntu apt program
#1
I've been able to copy/paste my way to a semi completed program. It helps with with the apt command, displays descriptions of the packages, download package.

Right now its set for a 'dry run' and is in a testing mode. Please tell me if i am doing anything wrong. I'm sure i could be doing some things a better way. Let me know. Thanks.






from Tkinter import *
from ttk import *
import subprocess, re, webbrowser, pexpect, time, tkFont
from urllib import urlopen
from bs4 import BeautifulSoup


class VerticalScrolledFrame(Frame):

    def __init__(self, parent, *args, **kw):
        Frame.__init__(self, parent, *args, **kw)            
        global canvas
        vscrollbar = Scrollbar(self, orient=VERTICAL)
        vscrollbar.pack(fill=Y, side=RIGHT, expand=FALSE)
        canvas = Canvas(self, bd=0, width=600, height=600, highlightthickness=0,
                        yscrollcommand=vscrollbar.set)
        canvas.pack(side=LEFT, fill=BOTH, expand=TRUE)
        vscrollbar.config(command=canvas.yview)
        canvas.xview_moveto(0)
        canvas.yview_moveto(0)

        self.interior = interior = Frame(canvas)
        interior_id = canvas.create_window(0, 0, window=interior,
                                           anchor=NW)

        def _configure_interior(event):
            size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
            canvas.config(scrollregion="0 0 %s %s" % size)
            if interior.winfo_reqwidth() != canvas.winfo_width():
                canvas.config(width=interior.winfo_reqwidth())
        interior.bind('<Configure>', _configure_interior)

        def _configure_canvas(event):
            if interior.winfo_reqwidth() != canvas.winfo_width():
                canvas.itemconfigure(interior_id, width=canvas.winfo_width())
        canvas.bind('<Configure>', _configure_canvas)

if __name__ == "__main__":

    class SampleApp(Tk):
        
        def __init__(self, *args, **kwargs):
            root = Tk.__init__(self, *args, **kwargs)
            self.frame = VerticalScrolledFrame(root)
            Label(self,text = "Apt-Rep: ").pack()
            self.person_ent = Entry(self)
            self.person_ent.pack()
            b = Button(self,text = "Search",command = self.list_apt)
            b.pack()

            
        
        def list_apt(self):
            output1 = ''
            person = self.person_ent.get()
            bashCommand = "apt-cache search " + person
            process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
            output = process.communicate()[0]
            process    
            self.frame.pack()
            for line in output.splitlines():
                link = Label(self.frame.interior, text=line, foreground="#0000ff", cursor="hand1", justify=LEFT, wraplength=600)
                link.bind('<Button-1>', lambda event, line = line: self.callback(line))
                link.pack(padx=10, ipady=5, anchor=W)
            
        
        def callback(self, line):
            seperator = Label(self.frame.interior, text="************************************************************************************")
            line = str(line)
            butt = re.sub("b'", "", line).split(' ', 1)[0]
            url = "https://packages.debian.org/stable/" + butt
            webpage = urlopen(url).read()
            soup2 = BeautifulSoup(webpage)
            titleSoup = soup2.findAll("div", {"id": "pdesc"})
            str_titleSoup = str(titleSoup)
            soup_list = ['<p>', '</p>', '<h2>', '</h2>', '</div>]', '\[<div id="pdesc">', '<pre>', '</pre>']
            desc = str_titleSoup
            for item in soup_list:
                item = str(item)
                line = re.sub(item, "", desc)
                desc = line
            link = Label(self.frame.interior, text=desc, anchor=W, justify=LEFT, wraplength=600)
            link.bind()
            seperator.pack(fill=X, expand=TRUE)
            title = Label(self.frame.interior, text=butt, justify=CENTER, wraplength=600)
            title.pack()
            q = tkFont.Font(title)
            q.configure(underline = True)
            title.configure(font=q)
            link.pack(fill=X, expand=TRUE)
            apt_link = Label(self.frame.interior, text="Download", foreground="#0000ff", cursor="hand1", justify=LEFT, borderwidth=2)
            apt_link.bind('<Button-1>', lambda event, butt = butt: self.apt_get(butt))
            apt_link.pack(padx=10, ipady=5, anchor=W)
            f = tkFont.Font(apt_link)
            f.configure(underline = True)
            apt_link.configure(font=f)
        

            
        def apt_get(self, butt):
            child = pexpect.spawn('sudo -k apt-get --dry-run install ' + butt)
            child.logfile = sys.stdout
            child.expect('password')
            child.sendline(pword)
            i = child.expect(['continue','Conf','newest'])
            if(i==0):
                child.sendline('Y')
            elif(i==1):
                print("\nConfirmed download")
            elif(i==2):
                print("\nAlready installed")
            child.expect(pexpect.EOF, timeout=None)

            
     
    pword = 'YOURPASSWORD'
    app = SampleApp()
    app.mainloop()
The things i want to work on are a pop-up window for a password prompt and being able to clear the scrollable frame.
I tried using the pack_forget() method but was unsuccessful.

This is what i  tried for the new window with the password prommpt.
        def confirm_pass(self, pass_get):
            print(pass_get)
            
        def get_pass(self):
            top = Toplevel()
            l = Label(top, text="Enter Password:")
            l.pack()
            pass_ent = Entry(top)
            pass_ent.pack()
            pass_get = pass_ent.get()
            b = Button(top,text = "Search",command = self.confirm_pass(pass_get))
            b.pack()
Reply
#2
Rather than including your whole project, could you write up a minimal snippet of code for your specific question(s)? This would probably be 20 or less lines of code, and the simplification would increase the chance of a helpful reply. (I don't do GUI code, so I'm minimally qualified to help here anyway.)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation Debian 10 Buster Environment - Python 3.x (MariaDB 10.4.21) | Working Connector? BrandonKastning 9 4,240 Jan-04-2022, 08:27 PM
Last Post: BrandonKastning
  mod-python in apache2 on debian 9 xstation 6 3,813 Jun-15-2020, 01:29 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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