我对Python很陌生,遇到了麻烦。如果您将iMessage联系人中的某个人添加到";FrstNm LstNm";。但是,我希望按钮名称、消息文本、收件人,以及可能的函数名称都是用户输入的。我知道我可能需要另一个窗口来进行实际的输入操作。所有这些都不会让我头疼,就像我如何获得所有的输入来反映要保存在数据库中的新保存按钮一样?我最终计划让用户能够添加更多的按钮,并从本质上复制整个按钮的过程。我完全被难住了,不知道从哪里开始。那会是什么样子?任何提示或资源都将非常有帮助,我们将不胜感激。
import PySimpleGUI as sg
import sys
import subprocess
from os import system, name
from playsound import playsound
msg1 = "On a Call" # I'd like the message sent to be user input
people = "FrstNm LstNm" # I'd like Nms to be user input
layout = [[sg.Button('ON CALL')]] #I'd like the button name 'ON CALL' to be user input
sg.SetOptions(font='Any')
sg.theme('LightBlue')
window = sg.Window('Text', auto_size_buttons=False, alpha_channel=0.85, keep_on_top=True,).Layout(layout)
repeat = 1
def onCall(): # Should/can the name of this function match input of button txt?
applescript = (
"""
set people to "{1}"
tell application "Messages"
repeat with myBuddy in buddies
--get properties of myBuddy
if name of myBuddy is in people then
send "{2}" to myBuddy
end if
end repeat
end tell
"""
.format(
repeat, people, msg1
)
)
args = [
item
for x in [("-e", l.strip()) for l in applescript.split("n") if l.strip() != ""]
for item in x
]
proc = subprocess.Popen(["osascript"] + args, stdout=subprocess.PIPE)
progname = proc.stdout.read().strip()
# Button Loop
while True:
event, values = window.Read()
if event == 'ON CALL':
sound(),onCall()
elif event== sg.WIN_CLOSED:
break
sys.exit()
def main():
onCall()
if __name__ == "__main__":
main()
尝试使用Multiline
作为多行输入,如消息和状态,并使用Combo
作为联系人的输入或选择。
from time import sleep
from threading import Thread
import PySimpleGUI as sg
def update_contact(contacts, contact):
if contact:
if contact not in contacts:
contacts.append(contact)
contacts = sorted(contacts)
combo.update(value=contact, values=contacts)
def send_message(window, contact, message):
sleep(1) # Simulate to send message
window.write_event_value('Sent', (contact, message))
font = ("Courier New", 11)
sg.theme("DarkBlue3")
sg.set_options(font=font)
contacts = []
layout = [
[sg.Combo(contacts, default_value='', size=30, expand_x=True, key="Contact"),
sg.Button("Send")],
[sg.Multiline("On a Call", size=(40,10), key='Message', text_color='white', background_color='green')],
[sg.Multiline('', size=(40, 5), key='Status', text_color='white', background_color='green')],
]
window = sg.Window('Title', layout, finalize=True)
combo, status = window['Contact'], window['Status']
combo.bind('<Return>', ' Return')
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == 'Contact Return':
contact = values['Contact'].strip()
update_contact(contacts, contact)
elif event == 'Send':
contact = values['Contact'].strip()
update_contact(contacts, contact)
message = values['Message'].strip()
if contact and message:
Thread(target=send_message, args=(window, contact, message), daemon=True).start()
window['Message'].update('')
elif event == 'Sent':
conatct, message = values[event]
message_formated = 'n'.join(map(lambda x: " "*2+x, message.split('n')))
status.update(f"Message sentnRecipient: {contact}nMessage : n{message_formated}")
window.close()