Python Forum
[Tkinter] output to canvas widget
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] output to canvas widget
#1
First post here on this new forum.

I'm trying to get a windows 'dir' command output to display in a canvas widget. Ultimately I want to built an interactive program than will display the output in the canvas widget; this is the first step to get it working.

from Tkinter import *
import subprocess

class window:


   def __init__(self, parent):
       self.frame=Frame(parent)
       self.frame.pack()
       self.gobutton = Button(self.frame, text="Go", command=self.dircall)
       self.gobutton.grid(row=0, column=1)
       self.canvas = Canvas(root, width = 100, height = 100, bg = "white")
       self.canvas.pack()
   def dircall(self):
       output=subprocess.call(["dir", "c:"], shell=True)
       self.canvas.create_text(25, 25, text=output, tags="text")


root = Tk()
root.geometry("200x200")
runit=window(root)
root.mainloop()
When started the GUI opens and then when the button is clicked the output is a 0 and the output from the dir command goes to the run output part of my IDE. I don't get and error, it just doesn't quite do what I want it to.

Any help pointing me to the solution would be appreciated.
Reply
#2
"0" means that the "dir"-command was processed successfully.
You could use check_output():


       output=subprocess.check_output(["dir", "c:"], shell=True)
Reply
#3
I modified a bit, you'll need a bigger Canvas or a smaller font
from Tkinter import *
# import subprocess
import os

class Window:
    def __init__(self, parent):
        self.frame = Frame(parent)
        self.frame.pack()
        self.gobutton = Button(self.frame, text="Go", command=self.dircall)
        self.gobutton.grid(row=0, column=1)
        self.canvas = Canvas(root, width=100, height=100, bg="white")
        self.canvas.pack()

    def dircall(self):
        # output = subprocess.call(["dir", "c:"], shell=True)
        # self.canvas.create_text(25, 25, text=output, tags="text")
        output = os.listdir('C:/')
        x = 5
        y = 10
        for line in output:
            print(line)
            self.canvas.create_text(x, y, text=line, tags='text', anchor='nw')
            y += 10


root = Tk()
root.geometry("200x200")
runit = Window(root)
root.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] canvas widget scroll issue chrisdb 2 3,779 Apr-07-2021, 05:48 AM
Last Post: chrisdb
  [Tkinter] How to erase previous output from the canvas? Pedroski55 4 4,406 Jul-05-2020, 10:25 PM
Last Post: Pedroski55
  How to place global tk text widget in class or on canvas puje 1 2,283 Jul-04-2020, 09:25 AM
Last Post: deanhystad
  The coordinates of the Entry widget (canvas) when moving berckut72 8 5,849 Jan-07-2020, 09:26 AM
Last Post: berckut72
  [Tkinter] Resizing image inside Canvas (with Canvas' resize) Gupi 2 25,026 Jun-04-2019, 05:05 AM
Last Post: Gupi

Forum Jump:

User Panel Messages

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