I was trying to call local variables outside far from a function that I made. I was playing with Tkinter in python. Here is the sample code. Is that possible to call variables outside function anywhere?
from tkinter import *
font1 = "Roboto 14"
window = Tk()
window.geometry("1200x800")
window.title("TEST")
canvas = Canvas(window, bg = "#FFFFFF", height = 800, width = 1200, bd = 0,
highlightthickness = 0)
canvas.place(x = 0, y = 0)
def load_auto_button_output ():
get_text_file = r"Links.txt"
read_text_file = open(get_text_file, 'r')
intermediate = read_text_file.read()
urlList = intermediate.split("\n")
s1 = urlList[0]
s2 = urlList[1]
txt_box.insert(0.0,
s1 + '\n' +
s2 + '\n'
)
def load_automatically_button ():
load_auto_button = Button(window, text = 'Automatically Load', font = font1 ,
width = 25, bd = 1, highlightthickness = 0,
relief = "ridge", command = load_auto_button_output)
load_auto_button.place(x = 540, y = 60)
load_automatically_button ()
txt_box = Text(window, bg = 'white', font = font1, height = 10, width = 70, bd = 3,
relief = "flat", wrap=WORD)
txt_box.place(x = 370, y = 500)
window.resizable(False, False)
window.mainloop()
Here you can see I made a button command function name load_auto_button_output
and I called all output inside text_box
, via txt_box.insert
.
Now, how do I call text_box.insert
outside of that load_auto_button_output
or how do I call s1
, s2
variables outside that function?
I have tried global but it's not working by my side
global s1, s2
then I had to return the function load_auto_button_output ()
, then it's automatically print values from the button without a click and nothing happen when I press the Automatically Load button.