如何解决视觉工作室 2019 中的 Ironpython
我一直在努力在 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())
解决方法
在课堂外使用 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,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,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,e):
sender.Foreground = Brushes.Black
if __name__ == '__main__':
Application().Run(WpfSampleWindow())
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。