简体   繁体   English

Python 在未安装 python 的计算机上使用脚本的 pyinstaller 版本时,脚本未更新文件内容

[英]Python Script not updating file contents while using a pyinstaller version of the script on a machine without python installed

So my issue right now is that I wrote a script that would update contents within a file each time you hit an arrow key.所以我现在的问题是我写了一个脚本,每次你按下箭头键时都会更新文件中的内容。 The script works great on my PC.该脚本在我的 PC 上运行良好。 I used pyinstaller in an attempt to create a straight forward standalone version of the script.我使用 pyinstaller 试图创建脚本的直接独立版本。 The problem right now is that the script is not seemingly updating their files, Within the console of the application, it does show that it is properly counting up the scores.现在的问题是脚本似乎没有更新他们的文件,在应用程序的控制台中,它确实表明它正在正确计算分数。 but upon opening the file it stays at 0. Files are not in read-only?: any ideas?但是在打开文件时它保持在0。文件不是只读的?:有什么想法吗? Code below:下面的代码:

import os
import time
import threading


from pynput import keyboard

def on_press(key):

    if (key == keyboard.Key.down):
        wR = open("Round Number.txt", "w")
        w1 = open("Team 1 Score.txt", "w")
        w2 = open("Team 2 Score.txt", "w")
        wR.write("1")
        w1.write("0")
        w2.write("0")
        print("All values have been reset")
    
    if (key == keyboard.Key.up):
        r = open("Round Number.txt", "r")
        previousScore = r.read()
        w = open("Round Number.txt", "w")
        newScore = str(int(previousScore) + 1)
        print("Round file has changed to " + newScore)
        w.write(newScore)
        #print(open("Round Number", "r").read())
    
    if (key == keyboard.Key.left):
        r = open("Team 1 Score.txt", "r")
        previousScore = r.read()
        w = open("Team 1 Score.txt", "w")
        newScore = str(int(previousScore) + 10)
        w.write(newScore)
        print("Team 1 now has " + newScore)
        #print(open("Team 1 Score.txt", "r").read())

    if (key == keyboard.Key.right):
        r = open("Team 2 Score.txt", "r")
        previousScore = r.read()
        w = open("Team 2 Score.txt", "w")
        newScore = str(int(previousScore) + 10)
        w.write(newScore)
        print("Team 2 now has " + newScore)
       # print(open("Team 2 Score.txt", "r").read())

def on_release(key):
    pass
    #print('{0} released'.format(
        #key))
    '''if key == keyboard.Key.esc:
        # Stop listener
        return False'''

#Collect events until released
with keyboard.Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()

# ...or, in a non-blocking fashion:
listener = keyboard.Listener(
    on_press=on_press,
    on_release=on_release)
listener.start()```

One of the main mistakes I immediately saw in your code was that you weren't closing the files once you were opening them.我在您的代码中立即看到的主要错误之一是您在打开文件后没有关闭它们。 I hadn't tested your code directly but I did modify it to use with as below.我没有直接测试你的代码,但我确实修改了它以with如下。 I tested it and it is working fine.我测试了它,它工作正常。 I also created an executable and that too is working fine with values being stored in the files.我还创建了一个可执行文件,它也可以很好地处理存储在文件中的值。

from pynput import keyboard

def on_press(key):

    if (key == keyboard.Key.down):
        with open("Round Number.txt", "w") as wR:
            wR.write("1")
        with open("Team 1 Score.txt", "w") as w1:
            w1.write("0")       
        with open("Team 2 Score.txt", "w") as w2:
            w2.write("0")

        print("All values have been reset")

    if (key == keyboard.Key.up):
        with open("Round Number.txt", "r") as r:
            previousScore = r.read()
        with open("Round Number.txt", "w") as w:
            newScore = str(int(previousScore) + 1)
            w.write(newScore)

        print("Round file has changed to " + newScore)
    
    if (key == keyboard.Key.left):
        with open("Team 1 Score.txt", "r") as r:
            previousScore = r.read()
        with open("Team 1 Score.txt", "w") as w:
            newScore = str(int(previousScore) + 10)
            w.write(newScore)
        print("Team 1 now has " + newScore)

    if (key == keyboard.Key.right):
        with open("Team 2 Score.txt", "r") as r:
            previousScore = r.read()
        with open("Team 2 Score.txt", "w") as w:
            newScore = str(int(previousScore) + 10)
            w.write(newScore)
        print("Team 2 now has " + newScore)

def on_release(key):
    pass

#Collect events until released
with keyboard.Listener(on_press=on_press,on_release=on_release) as listener:
    listener.join()

# ...or, in a non-blocking fashion:
listener = keyboard.Listener(on_press=on_press,on_release=on_release)
listener.start()

One main problem though is that I had to manually create the 3 files and not pressing Down at the beginning caused the program to crash.一个主要问题是我必须手动创建 3 个文件,并且在开始时没有按下Down导致程序崩溃。 Try resolving this too.也尝试解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 PyInstaller 将 python 3.7 脚本转换为 exe 时出错:ModuleNotFoundError - Error while converting python 3.7 script to exe using PyInstaller: ModuleNotFoundError 如果机器上安装了多个版本的python,如何告诉python脚本文件使用特定版本的python - How to tell python script files to use particular version of python if there are multiple versions of python are installed on the machine 使用pyinstaller时如何在python脚本中选择文件路径? - How to choose file path in python script when using pyinstaller? 在python脚本上使用pyinstaller会导致巨大的文件大小 - Using pyinstaller on a python script results in huge filesize Python - pyinstaller:读取脚本内的路径文件以在 pyinstaller 中使用 - Python - pyinstaller : Read path file inside script to use in pyinstaller 无法执行使用PyInstaller构建的python脚本 - Failed to execute python script built using PyInstaller 在 PC 上未安装 python 的情况下运行 python 脚本 - Running python script without python installed on pc 使用python脚本增加xml文件中的版本 - Increasing Version in xml file using python script 将文件(无扩展名)内容作为对象导入python脚本 - Importing file (without extension) contents into python script as object 在python脚本中读取tar文件内容而不解压它 - reading tar file contents without untarring it, in python script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM