Ongoing/PythonNET

Python for .NET : Windows.Forms Example 3

More Code 2019. 5. 15. 05:37


# Python for .NET

import sys
sys.path.append("..\\Sharp1\\bin\\Debug")

import clr
clr.AddReference("Forms1")

import Forms1
import System
import System.Windows.Forms
from PyQt5.QtWidgets import *


class pyForm1(Forms1.Form1):
def __init__(self):
self.Text = "Python for .NET"
self.button1.Click += self.pybutton1_Click
self.button2.Click += self.pybutton2_Click

def pybutton1_Click(self, sender, e):
self.button1_Click(sender, e, "button1 clicked")

def pybutton2_Click(self, sender, e):
self.button2_Click(sender, e, "button2 clicked")


class MainWindow(QMainWindow):
def __init__(self):
super().__init__()

# button1
self.button1 = QPushButton("button1", self)
self.button1.move(20, 20)
self.button1.clicked.connect(self.button1_clicked)

# button2
self.button2 = QPushButton("button2", self)
self.button2.move(20, 60)
self.button2.clicked.connect(self.button2_clicked)

# button3
self.button3 = QPushButton("button3", self)
self.button3.move(20, 100)
self.button3.clicked.connect(self.button3_clicked)

def button1_clicked(self):
myForm1 = pyForm1()
System.Windows.Forms.Application.EnableVisualStyles()
#System.Windows.Forms.Application.Run(myForm1)
myForm1.ShowDialog()

def button2_clicked(self):
QMessageBox.about(self, "PyQt Box", "Life is short\nYou need Python")

def button3_clicked(self):
System.Windows.Forms.MessageBox.Show("Hello .NET", ".NET Box")

def main():
print(sys.version)
print(sys.argv)

app = QApplication(sys.argv)
window = MainWindow()
window.setWindowTitle("Hi QMainWindow")
window.setGeometry(100, 100, 400, 300)
window.show()
app.exec()


if __name__ == '__main__':
main()




// Form1.cs

using System;
using System.Windows.Forms;

namespace Forms1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//InitializeMyComponent();
}

//private void InitializeMyComponent()
//{
// this.button1.Click += new System.EventHandler(this.button1_Click);
// this.button2.Click += new System.EventHandler(this.button2_Click);
//}

public void button1_Click(object sender, EventArgs e, string text)
{
this.textBox1.Text = text;
}

public void button2_Click(object sender, EventArgs e, string text)
{
this.textBox1.Text = text;
}
}
}




// Form1.Designer.cs

namespace Forms1
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
//
// button2
//
this.button2.Location = new System.Drawing.Point(93, 12);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
//
// textBox1
//
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox1.Location = new System.Drawing.Point(12, 41);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox1.Size = new System.Drawing.Size(263, 127);
this.textBox1.TabIndex = 2;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(287, 180);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

public System.Windows.Forms.Button button1;
public System.Windows.Forms.Button button2;
public System.Windows.Forms.TextBox textBox1;
}
}