Skip to main content
 首页 » 编程设计

python之如何在 pySimpleGUI 中读取文件输入然后将其传递给数字运算处理器

2025年05月04日24softidea

我想从 pySimpleGUI 获取输入,将其输入普通的 Python var,然后将其输入音乐处理器,因为我喜欢音乐。

我已经尝试为此使用 wxPython,但无法在不崩溃的情况下获得简单的 fileDialog。

    from pydub import AudioSegment 
    from os import listdir 
    import numpy as np 
    import math 
    import PySimpleGUI as sg 
 
    class Dankify(): 
            song_dir = "songs" 
            attenuate_db = 0 
            accentuate_db = 2 
 
            yeet = sg.Window('Dankify ALL THE THINGS!'). Layout([[sg.Text('Filename')], [sg.Input(), sg.FileBrowse()], [sg.OK(), sg.Cancel()] ]).Read() 
 
            event, values = yeet.Read() 
            yeet1 = event, values 
 
            def bass_line_freq(track): 
                    sample_track = list(track) 
 
                    # c-value 
                    est_mean = np.mean(sample_track) 
 
                    # a-value 
                    est_std = 3 * np.std(sample_track) / (math.sqrt(2)) 
 
                    bass_factor = int(round((est_std - est_mean) * 0.005)) 
 
                    return bass_factor 
 
                    songfile = yeet1 
                    for filename in listdir(songfile): 
                        sample = AudioSegment.from_mp3(songfile) 
                        filtered = sample.low_pass_filter(bass_line_freq(sample.get_array_of_samples())) 
 
                        combined = (sample - attenuate_db).overlay(filtered + accentuate_db) 
                        combined.export("exports/" + filename.replace(".mp3", "") + "-export.mp3", format="mp3") 

然而,它什么也不做,甚至不处理它。提醒一下,我正在使用一些开源代码,而且我是一个初学者,对所有这些工作原理一无所知,并且正在尝试构建真实的东西来获得经验。谢谢!

请您参考如下方法:

我猜你错过了“事件循环”。

尝试这样的事情,希望它有帮助。

import sys   
if sys.version_info[0] >= 3:   
    import PySimpleGUI as sg   
else:   
    import PySimpleGUI27 as sg   
 
layout = [[sg.Text('Your typed chars appear here:'), sg.Text('', key='_OUTPUT_') ],   
          [sg.Input(do_not_clear=True, key='_IN_')],   
          [sg.Button('Show'), sg.Button('Exit')]]   
 
window = sg.Window('Window Title').Layout(layout)   
 
while True:                 # Event Loop   
  event, values = window.Read()   
  print(event, values) 
  if event is None or event == 'Exit':   
      break   
  if event == 'Show':   
      # change the "output" element to be the value of "input" element   
      window.FindElement('_OUTPUT_').Update(values['_IN_']) 
 
window.Close()