Tkinter and printf in C ...

Bengt Richter bokr at accessone.com
Sun Jul 8 23:28:45 CEST 2001


On Sun, 08 Jul 2001 15:10:38 +0800, Chyr-Pyng Su
<cpsu at larc.ee.nthu.edu.tw> wrote:
>Thanks in advance to help me writing the GUI program.. Actually I intended to
>contruct a
>buttom so that when I press it, it will trigger a wrapped function originally
>written in C.
>And capture those dumped message, show them in ScrolledText() widget..
>
>Sorry, I can't figure out what the self.index is... besides, where should I
>place these codes..
>in the callback function or in top level... :O
>
I'm a complete tkInter newbie, but here is something I hacked together
to do what you want (I think). The button is kind of clunky (how do
you make it less tall without going to a bitmap?)

Of course you can hard code the command line for your C program in
place of the interactive input. BTW, you can just hit Enter instead
of clicking the button, because of the binding of <return>.

I borrowed liberally from the sample directories, but Guido and Matt
are not responsible ;-)
HTH

Watch out for some wrapped lines below.
"echo hello" (w/o quotes) might be a good command to try first
______________________________________________________________________

# tkDoCmd.py - Illustrates running an arbitrary command
#              via popen and displaying result
#
# WARNING - THIS IS NOT AN INTERACTIVE SHELL INTERFACE!!
# You can screw things up with the wrong command,
# so watch what you're typing!
#
import os
from Tkinter import *
from ScrolledText import ScrolledText

class App:
    def __init__(self, master):

        self.hd = Label(master,anchor=W,text='(No command
yet)',font='courier 10 bold')
        self.hd.pack(side=TOP,fill=X,expand=1)

        self.frame = Frame(master)
        self.frame.pack(side=BOTTOM)

        self.te = Entry(self.frame, width=40, relief=SUNKEN,
borderwidth=2)
        self.te.pack(side=LEFT)
        self.te.bind('<Return>', self.do_cmd)
        self.te.focus_set()

        self.sp = Label(self.frame,width=2)
        self.sp.pack(side=LEFT)
    
        self.do = Button(self.frame, text="Do Command",
command=self.do_cmd)
        self.do.pack(side=LEFT)

        self.tx = ScrolledText(master, 
                 {'height': 20,
                   'width': 80,
                   'wrap': 'none',
                   'relief': 'raised',
                   'font': 'courier 10',
                   'bd': 2})
        self.tx.pack(side=LEFT, fill=BOTH, expand=1)

    def do_cmd(self,ev=None):
        cmd = self.te.get()
        self.tx.delete(0.0,END)
        self.hd.config(text='Trying command "'+cmd+'" ...')
        p = os.popen(cmd, 'r')
        if not p:
            self.tx.insert(END,'Pipe Blew up??')
            return
        while 1:
            s=p.read(4096)
            if not s: break
            self.tx.insert(END,s)
        cmdStatus = p.close()
        if cmdStatus: cmdStatus= `cmdStatus`
        else: cmdStatus = 'Ok'
        self.hd.config(text='Result of command "'+cmd+'":
(Status='+cmdStatus+')')

root = Tk()
app = App(root)
root.mainloop()
__________________________________________________________________



More information about the Python-list mailing list