簡體   English   中英

Python 程序與 cmd 一起工作正常,當我 make.exe 它不工作

[英]Python program works fine with cmd, when I make .exe it doens't work

我在 cmd 中運行我的 python 程序,它工作正常。 我安裝了 pyinstaller 並將其更新到最新版本,從中制作了一個 .exe 文件。 如果我運行 .exe 文件是行不通的。 它正在打開一個 window 並在幾秒鍾內關閉而不運行該程序。 我已經錄制了我的屏幕,以便在 window 關閉之前為其拍照。 在此處輸入圖像描述

我已經制作了一個沒有任何導入的簡單程序來測試 py 安裝程序是否有效。 如果我運行那個 .exe 文件,它就會工作。

這是我的代碼;

#Server

import socket
import sys
import time
import pynput
from pynput.keyboard import Key, Controller

from datetime import datetime
sttime = datetime.now().strftime('%Y%m%d_%H:%M:%S - ')

keyboard = Controller()



print ('Opening program: TVV Sound Project - TCP to keyboard press V0.2 - Created by Schiettecatte Joris.')
print("")
print("")
print("")
with open('logging.txt', 'a') as logfile:
    logfile.write(sttime + 'Opening program: TVV Sound Project - TCP to keyboard press V0.2 - Created by Schiettecatte Joris.' + '\n')
    logfile.write('' + '\n')
    logfile.write('' + '\n')
    logfile.write('' + '\n')

try:
    with open('settings.txt', 'r') as settings_file:
        print("settings.txt has been read")
    with open('logging.txt', 'a') as logfile:
        logfile.write(sttime + 'settings.txt has been read' + '\n')

except FileNotFoundError:                                   #als settings.txt niet kan geopend worden zal dit volgende zaken doen.
    print ("settings.txt not found - creating file")
    with open('logging.txt', 'a') as logfile:
        logfile.write(sttime + 'settings.txt not found' + '\n')
   
else:
    with open('settings.txt', 'r') as settings_file:        
        IP_read = settings_file.read().split("\n")[0]
        IP = IP_read.split("#",1)[1]
        print("Settings - IP adresse: ", IP)
        with open('logging.txt', 'a') as logfile:
            logfile.write(sttime + 'IP adresse has been read: ' + IP + '\n')

    with open('settings.txt', 'r') as settings_file:        
        PORT_read = settings_file.read().split("\n")[1]
        PORT = PORT_read.split("#",1)[1]
        print("Settings - Port: ", PORT)
        with open('logging.txt', 'a') as logfile:
            logfile.write(sttime + 'PORT has been read: ' + PORT + '\n')
        PORT_INT = int(PORT)





    with open('settings.txt', 'r') as settings_file:        
        KEY1_read = settings_file.read().split("\n")[2]
        KEY1 = KEY1_read.split("#",1)[1]
        print("Settings - Key1: ", KEY1)
        with open('logging.txt', 'a') as logfile:
            logfile.write(sttime + 'Keyboard Key1 has been read: ' + KEY1 + '\n')

    print("")
    with open('logging.txt', 'a') as logfile:
        logfile.write('' + '\n')

 




s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = (IP, PORT_INT)
print("server will start on:",host)
print("")
with open('logging.txt', 'a') as logfile:
    logfile.write(sttime + 'server will start on: ' + IP + ':' + PORT + '\n')
    logfile.write('' + '\n')

s.bind(host)
print("Server done binding to host and port succesfully")
print("Server is waiting for incoming connections")
print("")
with open('logging.txt', 'a') as logfile:
    logfile.write(sttime + 'Server done binding to host and port succesfully' + '\n')
    logfile.write(sttime + 'Server is waiting for incoming connections' + '\n')
    logfile.write('' + '\n')


s.listen(1)
conn, addr=s.accept()
print(addr, "Has connected to the server and is now online.")
print("")

addr_ip = addr[0]
addr_port_int = addr[1]
addr_port = str(addr_port_int)
with open('logging.txt', 'a') as logfile:
    logfile.write(sttime + addr_ip + ':' + addr_port + ' Has connected to the server and is now online.' + '\n')
    logfile.write('' + '\n')


with conn:
    print('Connected by', addr)
    while True:
        try:
            incoming_message = conn.recv(1024)
            incoming_message_decode = incoming_message.decode()

            #print('niet decode:',incoming_message)
            #print(type(incoming_message))
            #print (len(incoming_message))

            #print('decoded:',incoming_message_decode)
            #print(type(incoming_message_decode))
            #print (len(incoming_message_decode))

            incoming_message_decode_trimmed = incoming_message_decode[0:8]
            print('Trimmed incoming decoded message: ',incoming_message_decode_trimmed)
            with open('logging.txt', 'a') as logfile:
                logfile.write(sttime + 'Trimmed incoming decoded message: ' + incoming_message_decode_trimmed + '\n')
                #print (len(incoming_message_decode_trimmed))


            if incoming_message_decode_trimmed == "string01":
                print("String meets the requirements")
                keyboard.press(KEY1)
                keyboard.release(KEY1)
                print("Key is pressed: ", KEY1)
                print("")
                with open('logging.txt', 'a') as logfile:
                    logfile.write(sttime + 'Key is pressed: ' + KEY1 + '\n')
                    logfile.write('' + '\n')

解決了。 看起來我使用的 pyinstaller 版本有一些錯誤。 現在我用的是1.6、8。 它工作正常。

pip install pynput==1.6.8

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM