В настоящее время я пытаюсь создать отдельные кнопки из списка вывода в другом скрипте python. У меня сейчас проблема в том, что я хочу создать отдельные кнопки для каждого значения в списке, но вместо этого все значения помещаются в 1 кнопку.
Любая помощь приветствуется.
test.py:
def function1():
print('Hi')
def function2():
Greetings = ['hi there', 'ho there', 'hey there']
print(*Greetings, sep='\n')
def function3():
print('Hello World!')
if __name__ == '__main__':
function1()
function2()
function3()
GUI Применение:
import sys
import tkinter as tk
class getHellofromtest(object):
def __init__(self):
#clear before values are populated
self.result = ''
def write(self, text):
#have to use += because one 'print()' executes 'sys.stdout' many times
self.result += text
def getHellos():
import test
"""
Displays greetings obtained from test.py onto GUI application
"""
# keep original `sys.stdout
old_stdout = sys.stdout
# redirect to class which has `self.result`
sys.stdout = getHellofromtest()
# it will execute only `function2`
test.function2()
# assign result to label (after removing ending "\n")
btn1['text'] = sys.stdout.result.strip()
# set back original `sys.stdout
sys.stdout = old_stdout
master = tk.Tk()
btn1 = tk.Button(master, text='List of Hellos')
btn1.pack()
btn = tk.Button(master, text="Get Targets", command=getHellos)
btn.pack()