Que vous ayez besoin d'une interface graphique sophistiquée pour votre application Python, que vous ayez une application de bureau Python existante qui nécessite des fonctionnalités supplémentaires ou que vous soyez principalement intéressé par un thème différent qui rafraîchira votre application, nous sommes là pour vous. Tout cela est possible avec l'aide de Telerik UI pour les contrôles WinForms.
Vous avez bien lu! IronPython et le dernier Visual Studio 2019 ont rendu tout cela possible! Ils permettent aux applications de bureau Python de fonctionner avec les contrôles WinForms. Et surtout, travaillez avec la Telerik UI pour la suite WinForms et tous les goodies qui sortent du sac.
Iron Python
IronPython est une puissante variante open-source de Python . Il s'agit d'une adaptation du langage de programmation Python qui s'exécute sur le framework .NET de Microsoft. IronPython peut utiliser les bibliothèques .NET Framework et Python, et d'autres langages .NET peuvent utiliser le code Python tout aussi facilement.
Après avoir installé la dernière version d'IronPython, vous pouvez ouvrir Visual Studio 2019 qui vient avec un projet de modèle intégré pour «Application IronPython Windows Forms» et créez votre première application.
et utilisations
dont nous aurons besoin pour notre exemple d'application.
import
clr
import
random
clr.AddReference (
'System.Drawing'
)
clr. AddReference (
'System.Windows.Forms'
)
clr.AddReference (
'Telerik.WinControls'
)
clr.AddReference (
'Telerik.WinControls.UI'
)
clr.AddReference (
'Telerik.WinControls.ChartView'
)
clr.AddReference (
'Telerik.WinControls.Themes.Fluent'
)
clr. AddReference (
'TelerikCommon'
)
clr.AddReference ([196590] 17] «Telerik.WinControls.GridView»
)
from
System.Drawing
import
*
[19659002] de
System.Windows.Forms
import
*
from
Telerik.WinControls
import
] *
de
Telerik.WinControls.UI
import
*
from
Telerik.Charting
import
*
from
Telerik.WinControls.Themes
import
*
Voyons maintenant comment ajouter un Contrôle radGridView, contenant un ensemble de colonnes différentes de notre formulaire.
#Define RadGridView
self
.radGrid
=
RadGridView ()
auto
.radGrid.BestFit Colonnes ()
self
.radGrid.ForeColor
=
Color.Black
self
. radGrid.Dock
=
DockStyle.Fill
#Define Columns
self
.decimalColumn
=
GridViewDecimalColumn ()
self
.textBoxColumn
=
GridViewTextBoxColumn ()
[1965918002] [1945918002] self
.colorColumn
=
GridViewColorColumn ()
self
.checkBoxColumn
= [1965901390] GridViewCheckBoxColumn () [194514] 19659002] auto
.ratingColumn
=
GridViewRatingColumn ()
self
.decimalColumn.Head
=
"DecimalColumn "
soi
.textBoxColumn.HeaderText
=
" Text "
self
.colorColumn. HeaderText
=
"ColorColumn"
self
.checkBoxColumn.HeaderText
=
"CheckBoxColumn"
self
.ratingColumn.HeaderText
=
"RatingColumn"
self
.radGrid.Columns.Add (
self
.decimalColumn)
self
.radGrid.Columns.Add (
self
.textBoxColumn)
self
.radGrid.Columns.Add (
self
.colorColumn)
self
.radGrid.Columns.Add ([19659127] self
.checkBoxColumn)
self
.radGrid.Columns.A dd (
self
.ratingColumn)
self
.Controls.Add (
self
.radGrid)
#Populate Rows
for
index
in
range (
10
):
self
.radGrid.Rows.Add (index,
"Sample Text"
+
str (index), Color.FromArgb ( random.randint (
1
255
), random.randint (
1
255
), random.randint (
1
255
)), CheckState.Checked, random.randint (
1
100
))
Le résultat de cet exercice est un radGridView qui a les colonnes suivantes: GridViewDecimalColumn, GridViewTextBoxColumn, GridViewColorColumn, GridViewCheckBoxColumn, GridViewRatingColumn.
fluent [19659043] =
FluentTheme ()
self
.ThemeName
=
fluent.ThemeName
self
.radGrid.ThemeName
=
fluent.ThemeName
S'abonner à des événements et mettre en œuvre votre logique d'entreprise
Si vous vous demandez comment l'abonnement à des événements fonctionne, c'est plus facile que vous ne le pensez . Voici un exemple de création d'un contrôle RadButton et d'abonnement à son événement Click
.
#Define RadButton1
self
.myButton1
=
RadButton ()
self
.myButton1.Text
=
"RadButton1"
self
.myButton1.Click
+
=
self
.OnButton1Click
self
.Controls.Add ([19659127] self
.myButton1)
Ensuite, comme vous le savez, vous devrez définir votre logique OnButtonClick.
def
OnButton1Click (
self
sender, args):
Logique #TODO OnClick
Jusqu'ici tout va bien. Examinons maintenant un exemple plus complexe lié aux événements en utilisant le radGridView de notre exemple précédent. Nous allons implémenter une logique dans l'événement CellFormatting afin de remplir les cellules de GridViewRatingColumn avec des couleurs différentes. La valeur à l'intérieur des cellules Rating peut être comprise entre 0 et 100. Si la valeur est <50, nous remplirons ces cellules en rouge, sinon nous utiliserons la couleur Aqua. Nous pouvons obtenir ce résultat avec le code suivant.
def
OnRadGridCellFormatting (
self
sender, args):
if [19659013] args
est
et non
Aucun
:
if
args.Column.HeaderText
=
=
"RatingColumn"
:
if
args.CellElement.RowInfo.Cells [
4
]. Value
n'est pas
Aucun
:
if
args.CellElement.Value>
50
:
args.CellElement.DrawFill
=
True
args.CellElement.ForeColor
= [19659013] Color.Blue
args.CellElement.NumberOfColors
=
1
args. CellElement.BackColor
=
Color.Aqua
else
:
[19659254] args.CellElement.DrawFill
=
True
args.CellElement.ForeColor
=
Color.Yellow
args.CellElement.NumberOfColors
=
1
args.CellElement.BackColor
=
Couleur .Red
else
:
args.CellElement.ResetValue (LightVisualElement.DrawFillProperty, ValueResetFlags.Local)
args.CellElement.ResetValue (LightVisualElement.Flags3. 19659002]
args.CellElement.ResetValue (LightVisualElement. ValueResetFlags.Local)
Le résultat final de notre effort CellFormatting ressemble à ça.
#Define RadChartView
self
.chartView
=
RadChartView ()
self
.chartView.Size
=
Size (
290
160
)
self
.chartView.Dock
=
DockStyle.Fill
#Define BarSeries and CategoricDataPoints
[19659002] self
.barSeries
=
BarSeries (
"Performance"
"RepresentativeName"
)
self
.barSeries.Name
=
"Q1"
self
.categoricDataPoint1
=
CategoricalDataPoint ([19659155] 177
"Harley"
)
self
.categoricDataPoint2
=
CategoricalDataPoint (
128
"White"
)
[19659002] soi
.categoricDataPoint3
=
CategoricalDataPoint (
143
"Smith"
)
self
.categoricDataPoint4
=
CategoricalDataPoint (
111
"Jones"
)
self [19659018] .barSeries.DataPoints.Add (
self
.categoricDataPoint1)
self
.barSeries.DataPoints.Add (
self
.categoricDataPoint2 )
self
.barSeries.DataPoints.Add (
self
.categoricDataPoint3)
self
.barSeries.DataPoints.Add (
self
.categoricD ataPoint4)
self
.chartView.Series.Add (
self
.barSeries)
self [19659018] .Controls.Add (
self
.chartView)
Le résultat est celui que vous pourriez vous attendre d'une interface utilisateur Telerik standard pour une application WinForms.
Source link