【问题标题】:Ironpython in visual studio 2019Visual Studio 2019 中的 Ironpython
【发布时间】:2021-02-05 00:03:35
【问题描述】:

我一直在努力创建一个在 Visual Studio 2019 中使用 IronPython 加载文件的按钮。控制台窗口弹出 1 秒然后消失。

import wpf

clr.AddReference('IronPython.Wpf') #Add a reference for the ' System.Windows.Forms' namespace and etc..
clr.AddReference('System.IO')
clr.AddReference('Systemm.Drawing')
clr.AddReference('System.Reflection')
clr.AddReference('System.Threading')
clr.AddReference('System.Windows.Forms')

class MyWindow(Window):

    def __init__(self):
        wpf.LoadComponent(self, 'WpfApplication1.xaml')
        
    def Button_Click(self, sender, e):
         #Folder path of variables
        FOLDER_PATH = r'C:\\Users\\Desktop\\waveform1' #Back slash will be treated as escape character
        # Define a funhction to print out the file name
        # Input parameter will be dir
    def listDir(dir):
        fileNames = os.listdir(dir)
        for fileName in fileNames:
            print('File Name: ' +fileName)
            print('Folder Path: ' + os.path.abspath(os.path.join(dir,fileName)))

        if __name__ == '__main__':
            Application().Run(MyWindow())

【问题讨论】:

  • 能否请您至少使用正确的屏幕截图而不是物理屏幕的倾斜照片?或者更好的是,只需复制并粘贴错误消息 text
  • 为什么if __name__ == '__main__':class MyWindow 里面?是故意的吗?

标签: python visual-studio-code ironpython ironpython-studio


【解决方案1】:

在课堂外使用if __name__ == '__main__':

import wpf
clr.AddReference('IronPython.Wpf') #Add a reference for the ' System.Windows.Forms' namespace and etc..
clr.AddReference('System.IO')
clr.AddReference('Systemm.Drawing')
clr.AddReference('System.Reflection')
clr.AddReference('System.Threading')
clr.AddReference('System.Windows.Forms')

class MyWindow(Window):

    def __init__(self):
        wpf.LoadComponent(self, 'WpfApplication1.xaml')
        
    def Button_Click(self, sender, e):
            #Folder path of variables
        FOLDER_PATH = r'C:\\Users\\Desktop\\waveform1' #Back slash will be treated as escape character
        # Define a funhction to print out the file name
        # Input parameter will be dir
    def listDir(dir):
        fileNames = os.listdir(dir)
        for fileName in fileNames:
            print('File Name: ' +fileName)
            print('Folder Path: ' + os.path.abspath(os.path.join(dir,fileName)))

if __name__ == '__main__':
    Application().Run(MyWindow())

我举个例子

import wpf

import FlippingGame

from System.Windows import Application, Window, Visibility
from System.Windows.Media import Brushes

class WpfSampleWindow(Window):
    def __init__(self):
        wpf.LoadComponent(self, 'PyWpfSample.xaml')
        self.game = FlippingGame.FlippingGame()
    
    def flipButton_Click(self, sender, e):
        wager = self._getWager()
        if not wager:
            return

        guess = "H" if self.guessHeadsButton.IsChecked else "T"
        won, toss = self.game.flip(guess, wager)

        self._showToss(won, toss)
        self._showBankroll()
        self._maybeEndGame()
    
    def Window_Loaded(self, sender, e):
        self._showBankroll()

    def _getWager(self):
        try:
            wager = int(self.wagerBox.Text)
        except ValueError as v:
            self.wagerBox.Foreground = Brushes.Red
            self._showError("Wager must be a number.")
            return
        else:
            self._hideError()
            self.wagerBox.Foreground = Brushes.Black

        if wager < 1:
            self.wagerBox.Foreground = Brushes.Red
            self._showError("Wager must be at least 1 credit.")
            return

        if wager > self.game.bankroll:
            self.wagerBox.Foreground = Brushes.Red
            self._showError("Wager cannot be more than your bankroll.")
            return

        return wager
    
    def _showError(self, error):
        self.errorLabel.Content = error
        self.errorLabel.Visibility = Visibility.Visible

    def _hideError(self):
        self.errorLabel.Visibility = Visibility.Collapsed

    def _showToss(self, won, toss):
        self.resultLabel.Content = toss
        self.resultLabel.Foreground = Brushes.Green if won else Brushes.Red

    def _showBankroll(self):
        self.bankrollLabel.Content = str(self.game.bankroll)

    def _maybeEndGame(self):
        if self.game.bankroll <= 0:
            self._showToss(False, 'X')

            self.flipButton.IsEnabled = False
            self.wagerBox.IsEnabled = False
            self.guessHeadsButton.IsEnabled = False
            self.guessTailsButton.IsEnabled = False
    
    def wagerBox_GotFocus(self, sender, e):
        sender.Foreground = Brushes.Black

if __name__ == '__main__':
    Application().Run(WpfSampleWindow())

【讨论】:

  • 它仍然显示消息:The progress' [2532] ipwy32.exe has exited with code 1
  • 你能详细说明一下吗?
  • 你在哪里得到Window 对象你没有导入它
猜你喜欢
  • 1970-01-01
  • 2020-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-09
  • 2021-10-04
相关资源
最近更新 更多