Python Forum
How to work with multiple files and tkinter?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to work with multiple files and tkinter?
#1
I have a beginners question about importing files and not being able to use a function in this file because it can't 'look back'.

I have two files. main.py:
from tkinter import *
from function import drawLine
master = Tk()

canvas_width = 80
canvas_height = 40
w = Canvas(master, 
           width=canvas_width,
           height=canvas_height)
w.pack()

y = int(canvas_height / 2)

drawLine()

mainloop()
and function.py:
def drawLine():
	w.create_line(0, y, canvas_width, y, fill="#476042")
I understand that 'function.py' cannot look into 'main.py'. How to write a function in 'function.py' that draws a line in the 'main.py' canvas? I hope someone can teach me about the way people work usually with drawing something on a canvas that is in another file :)
Reply
#2
Define the function so that it takes arguments and pass those when you call it.
Reply
#3
Aahh Thank you for the sugestion! :) After a lot of trying I found a way to do what I want.

main.py:
from tkinter import *
from function import drawLine

master = Tk()
w = Canvas(master, 
           width=80,
           height=40)
w.pack()

drawLine(10, 10, 100, 10, w)

mainloop()
function.py:
def drawLine(x1, y1, x2, y2, w):
	w.create_line(x1, y1, x2, y2)
Reply
#4
But having said that, do you really need the function in the first place? I mean, it doesn't really give you anything in this case, other than one unnecessary level of indirection.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open and read multiple text files and match words kozaizsvemira 3 3,782 Jul-07-2021, 11:27 AM
Last Post: Larz60+
  Plotting Multiple files ! Helen_145 1 229 Jun-26-2021, 03:28 PM
Last Post: snippsat
  Reading Multiple text Files in pyhton Fatim 1 254 Jun-25-2021, 01:37 PM
Last Post: deanhystad
  Running script on multiple files Afrodizzyjack 1 387 May-14-2021, 10:49 PM
Last Post: Yoriz
  Load the data from multiple source files to one table amy83 2 418 Apr-27-2021, 12:33 AM
Last Post: Pedroski55
  Rename Multiple files in directory to remove special characters nyawadasi 9 796 Feb-16-2021, 09:49 PM
Last Post: BashBedlam
  Can not get tkinter to work in Windows 10 via python 3.9 idris03 1 587 Nov-03-2020, 03:22 AM
Last Post: deanhystad
  Complex word search multiple files Kristenl2784 0 522 Jul-18-2020, 01:22 PM
Last Post: Kristenl2784
  Python: Automated Script to Read Multiple Files in Respective Matrices Robotguy 7 1,451 Jul-03-2020, 01:34 AM
Last Post: bowlofred
  Multiple excel files Kristenl2784 6 987 Jun-12-2020, 06:24 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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