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
  python convert multiple files to multiple lists MCL169 6 1,531 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  splitting file into multiple files by searching for string AlphaInc 2 878 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  Merging multiple csv files with same X,Y,Z in each Auz_Pete 3 1,146 Feb-21-2023, 04:21 AM
Last Post: Auz_Pete
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 955 Feb-15-2023, 05:34 PM
Last Post: zsousa
  Find duplicate files in multiple directories Pavel_47 9 3,067 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  Python: re.findall to find multiple instances don't work but search worked Secret 1 1,204 Aug-30-2022, 08:40 PM
Last Post: deanhystad
  Extract parts of multiple log-files and put it in a dataframe hasiro 4 2,081 Apr-27-2022, 12:44 PM
Last Post: hasiro
  Search multiple CSV files for a string or strings cubangt 7 7,995 Feb-23-2022, 12:53 AM
Last Post: Pedroski55
  Rename part of filename in multiple files atomxkai 7 7,326 Feb-18-2022, 10:03 PM
Last Post: atomxkai
  Process multiple pdf files Spartan314 1 1,315 Oct-27-2021, 10:46 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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