This article teaches you to develop a Python desktop application!

IronPython and the latest Visual Studio 2019 make this possible, and they enable Python desktop applications to be used with WinForms controls. More importantly, Telerik UI For WinForms suite has some ready-made functions to help you achieve the functions you want!

Iron Python

IronPython is a powerful open source version of Python. It is an adaptation of the Python programming language running on the Microsoft .NET framework. IronPython can use the .NET Framework and Python libraries, and other .NET languages ​​can also easily use Python code.

After installing the latest version of IronPython, you can open Visual Studio 2019, which comes with a built-in template project for "IronPython Windows Forms Application" and create your first application.

 

 

 

 

How to integrate Telerik RadGridView with modern Fluent themes into your application

First, you need to install Telerik UI for WinForms UI component suite - from which you can click to download , and then add the required Telerik assembly to the project folder in order to use the WinForms RadGridView, FluentTheme and RadChartView (spoiler alert, later Will need it) control.

 

 

To reference a binary file in an application, import the clr library and then use the addReference method. Here you can see how to add the reference and usage required for the sample 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('Telerik.WinControls.GridView')

from System.Drawing import *

from System.Windows.Forms import *

from Telerik.WinControls import *

from Telerik.WinControls.UI import *

from Telerik.Charting import *

from Telerik.WinControls.Themes import *`

Now, let's take a look at how to add a radGridView control, which contains a bunch of different columns.

`#Define RadGridView

self.radGrid = RadGridView()

self.radGrid.BestFitColumns()

self.radGrid.ForeColor = Color.Black

self.radGrid.Dock = DockStyle.Fill

Define Columns

self.decimalColumn = GridViewDecimalColumn()

self.textBoxColumn = GridViewTextBoxColumn()

self.colorColumn = GridViewColorColumn()

self.checkBoxColumn = GridViewCheckBoxColumn()

self.ratingColumn = GridViewRatingColumn()

self.decimalColumn.HeaderText = "DecimalColumn"

self.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(self.checkBoxColumn)

self.radGrid.Columns.Add(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))

`

The result is a radGridView with the following columns: GridViewDecimalColumn, GridViewTextBoxColumn, GridViewColorColumn, GridViewCheckBoxColumn, GridViewRatingColumn.

 

 

As shown in the figure, if you want to apply the theme to the control, you can do it as simple as that.

`fluent = FluentTheme()

self.ThemeName = fluent.ThemeName

self.radGrid.ThemeName = fluent.ThemeName`

Subscribe to events and implement your business logic

This is an example of creating a RadButton control and subscribing to its Click event.

`#Define RadButton1

self.myButton1 = RadButton()

self.myButton1.Text = "RadButton1"

self.myButton1.Click += self.OnButton1Click

self.Controls.Add(self.myButton1)`

Then, you will have to define OnButtonClick logic.

`def OnButton1Click(self, sender, args):

TODO OnClick logic`

Now, we use the radGridView in the previous example to see a more complex event-related example. We will implement some logic in the CellFormatting event to fill the cells in the GridViewRatingColumn with different colors. The value in the Rating unit can be between 0 and 100, if the value is <50, we will fill these units with red, otherwise we will use the Aqua color. We can achieve this result using the following code:

`def OnRadGridCellFormatting(self, sender, args):

if args is not None :

if args.Column.HeaderText == "RatingColumn" :

if args.CellElement.RowInfo.Cells[4].Value is not None :

if args.CellElement.Value > 50 :

args.CellElement.DrawFill = True

args.CellElement.ForeColor = Color.Blue

args.CellElement.NumberOfColors = 1

args.CellElement.BackColor = Color.Aqua

else :

args.CellElement.DrawFill = True

args.CellElement.ForeColor = Color.Yellow

args.CellElement.NumberOfColors = 1

args.CellElement.BackColor = Color.Red

else :

args.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local)

args.CellElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local)

args.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty, ValueResetFlags.Local)

args.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local)`

The final result of CellFormatting is this:

 

 

RadChartView's Little Bonus Setup example

As you can see in the radGridView example, it is very easy to use the Telerik control. Let's look at another example of setting the radChartView control using BarSeries.

`#Define RadChartView

self.chartView = RadChartView()

self.chartView.Size = Size(290, 160)

self.chartView.Dock = DockStyle.Fill

Define BarSeries and CategoricDataPoints

self.barSeries = BarSeries("Performance", "RepresentativeName")

self.barSeries.Name = "Q1"

self.categoricDataPoint1 = CategoricalDataPoint(177, "Harley")

self.categoricDataPoint2 = CategoricalDataPoint(128, "White")

self.categoricDataPoint3 = CategoricalDataPoint(143, "Smith")

self.categoricDataPoint4 = CategoricalDataPoint(111, "Jones")

self.barSeries.DataPoints.Add(self.categoricDataPoint1)

self.barSeries.DataPoints.Add(self.categoricDataPoint2)

self.barSeries.DataPoints.Add(self.categoricDataPoint3)

self.barSeries.DataPoints.Add(self.categoricDataPoint4)

self.chartView.Series.Add(self.barSeries)

self.Controls.Add(self.chartView)`

The result is the same as the Telerik UI for WinForms application you would expect.

 

Recently, many friends consulted about Python learning issues through private messages. To facilitate communication, click on the blue to join the discussion and answer resource base by yourself

 

Guess you like

Origin blog.csdn.net/weixin_43881394/article/details/112239429