创建具有文本框的PowerShell GUI

您是否具有需要接受文本输入的PowerShell GUI?也许您希望能够复制并粘贴计算机,IP地址,用户名或其他数据的列表。我将向您展示如何在PowerShell GUI中添加文本框来处理该输入。如果您还没有创建PowerShell GUI,但是对此感兴趣,请查看我以前的文章中有关如何制作简单GUI的文章。


让我们开始分解实现此目标所需的代码部分。我喜欢将输入框代码放在函数中,但是您可能会决定不想这样做。要查看整个代码,请查看我的GitHub

我们需要做的第一件事是通过.NET Windows窗体命名空间创建窗体。在这里,我们在窗口标题中指定所需的文本,以及所需的大小。某些可选设置是使窗口开始于屏幕中心,以防止调整窗口大小并将该窗口置于其他打开的窗口之上。

1个
2
3
4
5
6
7
8
9
### Creating the form with the Windows forms namespace
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Enter the appropriate information' ### Text to be displayed in the title
$form.Size = New-Object System.Drawing.Size(310,625) ### Size of the window
$form.StartPosition = 'CenterScreen'  ### Optional - specifies where the window should start
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedToolWindow  ### Optional - prevents resize of the window
$form.Topmost = $true  ### Optional - Opens on top of other windows

接下来,我们将在窗口底部添加一个确定按钮。输入数据后,用户将单击“确定”按钮将输入传递到GUI的下一步。

1个
2
3
4
5
6
7
8
### Adding an OK button to the text box window
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(155,550) ### Location of where the button will be
$OKButton.Size = New-Object System.Drawing.Size(75,23) ### Size of the button
$OKButton.Text = 'OK' ### Text inside the button
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

然后,如果用户决定不运行GUI,我们将添加一个取消按钮。

1个
2
3
4
5
6
7
8
### Adding a Cancel button to the text box window
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(70,550) ### Location of where the button will be
$CancelButton.Size = New-Object System.Drawing.Size(75,23) ### Size of the button
$CancelButton.Text = 'Cancel' ### Text inside the button
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

为了避免混淆并帮助用户了解要输入什么类型的数据,我们将添加标签。该标签将由调用函数时使用的参数定义。如果要对标签文本进行硬编码,只需将$ Input_Type替换为要显示的文本。

1个
2
3
4
5
6
7
8
9
### Putting a label above the text box
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,10) ### Location of where the label will be
$label.AutoSize = $True
$Font = New-Object System.Drawing.Font("Arial",12,[System.Drawing.FontStyle]::Bold) ### Formatting text for the label
$label.Font = $Font
$label.Text = $Input_Type ### Text of label, defined by the parameter that was used when the function is called
$label.ForeColor = 'Red' ### Color of the label text
$form.Controls.Add($label)

GUI窗口的最后一部分是文本框,它将用于接受输入的文本。

1个
2
3
4
5
6
7
8
### Inserting the text box that will accept input
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40) ### Location of the text box
$textBox.Size = New-Object System.Drawing.Size(275,500) ### Size of the text box
$textBox.Multiline = $true ### Allows multiple lines of data
$textbox.AcceptsReturn = $true ### By hitting enter it creates a new line
$textBox.ScrollBars = "Vertical" ### Allows for a vertical scroll bar if the list of text is too big for the window
$form.Controls.Add($textBox)

将画龙点睛放在一起,使它们一起工作并清理输入的数据。

1个
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$form.Add_Shown({$textBox.Select()}) ### Activates the form and sets the focus on it
$result = $form.ShowDialog() ### Displays the form
 
### If the OK button is selected do the following
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    ### Removing all the spaces and extra lines
    $x = $textBox.Lines | Where{$_} | ForEach{ $_.Trim() }
    ### Putting the array together
    $array = @()
    ### Putting each entry into array as individual objects
    $array = $x -split "`r`n"
    ### Sending back the results while taking out empty objects
    Return $array | Where-Object {$_ -ne ''}
}
 
### If the cancel button is selected do the following
if ($result -eq [System.Windows.Forms.DialogResult]::Cancel)
{
    Write-Host "User Canceled" -BackgroundColor Red -ForegroundColor White
    Write-Host "Press any key to exit..."
    $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    Exit
}

呼叫文字方块

既然创建GUI文本框的代码已经完成,我们需要调用文本框并返回输入的数据。我将分享一些有关如何利用我们刚创建的文本框的示例。下面的示例假定您已使用参数将文本框放入函数中。

1个
Function GUI_TextBox ($Input_Type){...}

这是一个如何使用文本框接受计算机名称列表的示例。本示例将仅输出输入到屏幕的计算机名称列表。为了使此功能有用,您将需要添加代码以使脚本对这些计算机名称起作用。也许您需要在计算机列表上执行电源操作或任何数量的管理员操作。

1个
2
3
4
5
6
7
8
9
10
11
12
13
14
15
###############################################################################
### Computer Name(s) example of how to utilize the GUI_TextBox function
###############################################################################
$Computers = GUI_TextBox "Computer Names(s):" ### Calls the text box function with a parameter and puts returned input in variable
$Computer_Count = $Computers | Measure-Object | % {$_.Count} ### Measures how many objects were inputted
 
If ($Computer_Count -eq 0){ ### If the count returns 0 it will throw and error
    Write-Host "Nothing was inputed..." -BackgroundColor Red -ForegroundColor White
    Return
}
Else { ### If there was actual data returned in the input, the script will continue
    Write-Host "Number of computers entered:" $Computer_Count -BackgroundColor Cyan -ForegroundColor Black
    $Computers
    ### Here is where you would put your specific code to take action on those computers inputted
}

这是一个如何使用文本框接受用户列表的示例。本示例将仅输出输入到屏幕的用户列表。为了使此功能有用,您将需要添加代码以使脚本对该用户执行操作。也许您需要禁用其Active Directory帐户或任何数量的管理员操作。

1个
2
3
4
5
6
7
8
9
10
11
12
13
14
15
###############################################################################
### User Name(s) example of how to utilize the GUI_TextBox function
###############################################################################
$Users = GUI_TextBox "User Names(s):" ### Calls the text box function with a parameter and puts returned input in variable
$User_Count = $Users | Measure-Object | % {$_.Count} ### Measures how many objects were inputted
 
If ($User_Count -eq 0){ ### If the count returns 0 it will throw and error
    Write-Host "Nothing was inputed..." -BackgroundColor Red -ForegroundColor White
    Return
}
Else { ### If there was actual data returned in the input, the script will continue
    Write-Host "Number of users entered:" $User_Count -BackgroundColor Cyan -ForegroundColor Black
    $Users
    ### Here is where you would put your specific code to take action on those users inputted
}

文字框在行动

完成后,您应该具有以下外观:

 

就是这样,感谢您的阅读!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: PyQt6是一个用于Python编程语言的GUI工具包,它允许开发人员在Windows、Mac和Linux等多个平台上创建功能强大的图形用户界面(GUI)应用程序。对于那些希望快速开发和实战的开发人员来说,PyQt6是一个非常有用的工具。 首先,你可以从官方网站上下载PyQt6的源码和文档。官方网站提供了最新的稳定版本和先前版本的下载选项。你可以根据自己的操作系统选择适当的版本进行下载。 在下载完成后,你可以按照文档中的说明进行安装。通常情况下,你只需要运行安装程序并按照步骤完成安装过程。一旦安装完成,你就可以开始在PyQt6中进行快速开发和实战。 PyQt6提供了丰富的GUI组件和工具,使得开发过程更加简单和高效。它包括了各种各样的控件,如按钮、文本框、标签、列表框等等,你可以通过简单的代码来创建和布局这些控件。此外,PyQt6还提供了各种丰富的功能,如绘图、动画、数据存储等,使得你可以轻松地开发出具有复杂功能和交互性的应用程序。 另外,PyQt6还提供了丰富的文档和示例代码。这些文档和示例代码将指导你如何正确地使用PyQt6的各种功能和组件。你可以通过阅读文档和运行示例代码来学习和掌握PyQt6的开发技巧和最佳实践。 总之,PyQt6是一个功能强大的GUI工具包,可以帮助你快速开发和实战。你可以从官方网站下载并安装PyQt6,并通过阅读文档和运行示例代码来学习和掌握它的使用方法。无论是初学者还是有经验的开发人员,PyQt6都是一个优秀的选择。 ### 回答2: 在进行PyQt6快速开发与实战之前,首先需要下载并安装PyQt6库。PyQt6是一个功能强大的Python框架,它提供了丰富的GUI编程工具和功能,可以帮助开发者快速构建界面丰富、交互性强的应用程序。 要下载PyQt6库,可以通过以下步骤进行: 1. 打开Python的官方网站(https://www.python.org/),并下载和安装Python解释器。请确保安装的是Python 3.x版本,因为PyQt6只支持Python 3。 2. 打开命令行终端(Windows可以使用Cmd、PowerShell等;Mac和Linux可以使用终端),输入以下命令安装PyQt6: ``` pip install PyQt6 ``` 这会自动从Python包索引下载并安装PyQt6库。如果你使用的是conda环境,也可以使用类似的命令进行安装。 3. 安装完成后,你就可以在Python脚本中导入并使用PyQt6库了。只需在脚本中加入以下语句即可: ```python from PyQt6.QtWidgets import QApplication, QMainWindow, QLabel # 在这里写下你的代码 ``` 这样,你就可以根据自己的需要使用PyQt6工具和功能进行开发了。 需要注意的是,PyQt6的下载速度可能会受到网络环境的影响。如果下载速度较慢,可以尝试更换源镜像,或者使用代理服务器来加快下载速度。 总之,通过以上步骤,你可以快速下载并安装PyQt6库,为实战开发提供强大的GUI编程工具和功能。希望这个回答对你有所帮助! ### 回答3: PYQT6是一种用于创建图形用户界面(GUI)应用程序的Python库。它基于了Qt框架,并提供了丰富的功能和工具,可以快速开发和部署可视化的应用程序。 要下载PYQT6快速开发与实战的内容,可以采取以下几个步骤: 1. 在互联网浏览器中搜索"PYQT6快速开发与实战",可以找到相关的网站或在线资源。 2. 访问可信赖的网站,如官方文档、编程论坛或云存储平台。 3. 在网站中搜索PYQT6快速开发与实战的下载链接或资源。 4. 点击下载链接,根据提示选择合适的版本和操作系统。 5. 下载完成后,可以解压缩文件并阅读其中的文档。通常会包含示例代码、教程和其他辅助材料。 6. 根据实际需要,可以在自己的开发环境中调用PYQT6库并开始实际的应用程序开发。 请注意,在下载PYQT6快速开发与实战的过程中要保持警惕,避免从不可靠的来源下载,以防止安全风险和恶意软件的感染。 最后,推荐在学习和使用PYQT6过程中,多查阅相关的文档和教程,充分利用PYQT6提供的功能和工具,以便更好地进行快速开发和实战。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值