Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在派生的IronPython类中与Windows窗体控件交互_C#_Winforms_Visual Studio 2013_Ironpython - Fatal编程技术网

C# 在派生的IronPython类中与Windows窗体控件交互

C# 在派生的IronPython类中与Windows窗体控件交互,c#,winforms,visual-studio-2013,ironpython,C#,Winforms,Visual Studio 2013,Ironpython,我正在学习如何将IronPython与C结合使用。我想将一个简单的windows窗体合并到我的应用程序中,所以我决定将一个窗体合并到一个单独的C#项目中,并将其编译为一个dll,我在python项目中引用了该dll。我想创建被引用的windows窗体类的一个子类,以便可以用python创建事件处理程序。现在我有了第一步: import clr import sys import threading import random import time clr.AddReference('Sy

我正在学习如何将IronPython与C结合使用。我想将一个简单的windows窗体合并到我的应用程序中,所以我决定将一个窗体合并到一个单独的C#项目中,并将其编译为一个dll,我在python项目中引用了该dll。我想创建被引用的windows窗体类的一个子类,以便可以用python创建事件处理程序。现在我有了第一步:

import clr
import sys
import threading 
import random
import time


clr.AddReference('System.Drawing')
clr.AddReference('System.Windows.Forms')
clr.AddReference('CustomForm')

from System.Drawing import *
from System.Windows.Forms import * 
from System.IO import *
from System import EventHandler
from CustomForm import *
from threading import Lock, Semaphore, Thread
from Queue import PriorityQueue


#Set up some global variables
NUM_DOCTORS = 3
MAX_CAPACITY = 10
PRIORITY_HIGH = 1
PRIORITY_MEDIUM = 2
PRIORITY_LOW = 3
PRIORITY_NONE = -1

#TODO: Fix all print statements once form can be referenced 

class MyForm(Form1):

    def __init__(self):
        # Create child controls and initialize form
        super(MyForm, self).__init__()
        self.InitializeComponent()
        self.input_button.Click += self.input_button_Click
        print "Here right now"
        self.admin = None
        self.started = False

    def setAdmin(self, ad):
        self.admin = ad

    def input_button_Click(self, sender, e):
        print "hello click"
        if not self.started:
            self.admin.start()
            self.started = not self.started

        name = self.input_box.Text
        #acquire the lock here, can't do it in the method or it will block
        self.admin.lock.acquire()
        self.admin.addPatient(name)
        self.admin.lock.release()
        print "is this working?"
        self.input_box.Clear()




class Patient():

    def __init__(self, patient_name):
        #set up the severity of the illness   
        self.name = patient_name
        #set up illness -> random time from 1-60 seconds
        self.illness = random.randint(1,60)
        self.priority = random.randint(1,3)


    def getIllness(self):
        return self.illness

    def getName(self):
        return self.name

    def getPriority(self):
        return self.priority


class Doctor(Thread):

    def __init__(self, administrator, patient = None):
        self.curr_patient = patient
        self.admin = administrator

    def healPatient(self):
        sleep_time = self.curr_patient.getIllness()
        #heal the patient by sleeping with how long it takes to cure illness 
        time.sleep(sleep_time)


    def setPatient(self, patient):
        self.curr_patient = patient

    def run(self):
        #grab a patient, heal them, and report the results 
        self.curr_patient = self.admin.getPatientFromQueue()
        healPatient()
        self.admin.reportResults(self.curr_patient)




class Administrator(Thread):

    def __init__(self, form):
        self.num_doctors = NUM_DOCTORS
        self.lock = Lock()
        self.patient_queue = PriorityQueue()
        self.hospital_status = []
        self.parent = form
        self.waiting_room = []
        #potential replacement for pq.qsize()
        self.num_patients = 0

    def run(self):
        #first time
        for i in range(NUM_DOCTORS):
            d = Doctor(self)
            d.start()
            num_doctors -= 1


    def getPatientFromQueue(self):
        #grab a patient from the queue safely 
        if self.patient_queue.qsize() >= 1:
            return patient_queue.get()
        else: 
            return None


    def reportResults(self, patient):
        #update master list
        with self.lock:
            self.hospital_status.remove(patient)
            self.admitPatient()
            #change this for outcome
            self.parent.status_box.Text += "\n" + patient.getName() + " is released"
            printStatus()
            self.num_doctors += 1

            while self.num_doctors < 0:
                d = Doctor(self)
                d.start()
                self.num_docters -= 1



    def addPatient(self, name):
        #add a patient if possible
        p = Patient(name)
        if len(self.hospital_status) < MAX_CAPACITY:
            self.hospital_status.append(p)
            self.patient_queue.put((p, p.getPriority()))
            self.parent.status_box.Text += "\nPlease come in."    
        else:
            #check the waiting list
            self.waiting_room.append(p)
            self.parent.status_box.Text += "\nPlease take a seat in the waiting room."

    def admitPatient(self):
        #check the waiting room -> admit patient if there otherwise do nothing
        if len(self.waiting_room) > 0:
            p = self.waiting_room.pop()
            self.addPatient(p)



    def printStatus(self):       
        #only called when lock is had
        for p in self.hospital_status:
            self.parent.status_box.Text += p.getName() + "-Priority-" + p.getPriority()
            #print p.getName() + "-Priority-" + p.getPriority()



Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)

form = MyForm()
admin = Administrator(form)
form.admin = admin



Application.Run(form)
导入clr
导入系统
导入线程
随机输入
导入时间
clr.AddReference('System.Drawing')
clr.AddReference('System.Windows.Forms')
clr.AddReference('CustomForm')
从系统。图形导入*
从System.Windows.Forms导入*
从System.IO导入*
从系统导入EventHandler
从CustomForm导入*
从线程导入锁、信号量、线程
从队列导入优先级队列
#设置一些全局变量
医生人数=3
最大容量=10
优先级高=1
优先级\中等=2
优先级低=3
优先级\无=-1
#TODO:一旦表单可以引用,请修复所有打印语句
类别MyForm(表格1):
定义初始化(自):
#创建子控件并初始化窗体
超级(MyForm,self)。\uuuuu init\uuuuuu()
self.InitializeComponent()
self.input\u button.Click+=self.input\u button\u Click
打印“现在就在这里”
self.admin=None
self.start=False
def setAdmin(自我,广告):
self.admin=ad
def输入按钮点击(自身、发送者、e):
打印“hello click”
如果不是自启动的:
self.admin.start()
自启动=非自启动
name=self.input\u box.Text
#在此处获取锁,无法在方法中执行,否则将被阻止
self.admin.lock.acquire()
self.admin.addPatient(姓名)
self.admin.lock.release()
打印“这有用吗?”
self.input_box.Clear()
类Patient():
定义初始(自我、患者姓名):
#确定疾病的严重程度
self.name=患者姓名
#设置疾病->1-60秒的随机时间
self.disease=random.randint(1,60)
self.priority=random.randint(1,3)
疾病定义(自我):
返病
def getName(self):
返回self.name
def getPriority(自):
返回自优先权
班级医生(线程):
定义初始化(自我、管理员、患者=无):
self.curr\u patient=患者
self.admin=管理员
def healPatient(自我):
睡眠时间=self.curr\u patient.getdisease()
#通过睡眠来治愈患者的疾病需要多长时间
时间。睡眠(睡眠时间)
def设置患者(自身,患者):
self.curr\u patient=患者
def运行(自):
#抓起一名患者,治愈他们,并报告结果
self.curr\u patient=self.admin.getPatientFromQueue()
healPatient()
自我管理报告结果(自我当前患者)
类管理员(线程):
定义初始(自我,形式):
self.num\u医生=num\u医生
self.lock=lock()
self.patient_queue=PriorityQueue()
self.hospital_status=[]
self.parent=form
自助等候室=[]
#pq.qsize()的潜在替代品
self.num_患者=0
def运行(自):
#第一次
对于范围内的i(数量):
d=医生(自我)
d、 开始()
医生人数-=1
def getPatientFromQueue(自):
#从队列中安全地抓取一名患者
如果self.patient_queue.qsize()>=1:
返回患者队列。获取()
其他:
一无所获
def报告结果(自我、患者):
#更新主列表
使用self.lock:
自我。医院_状态。移除(患者)
自我接纳病人()
#将此更改为结果
self.parent.status\u box.Text+=“\n”+patient.getName()+”已发布”
打印状态()
self.num_医生+=1
当self.num_医生<0时:
d=医生(自我)
d、 开始()
self.num_docters-=1
def addPatient(自我,姓名):
#如果可能,添加一名患者
p=患者(姓名)
如果len(自身医院状态)<最大容量:
自我医院状况附加(p)
self.patient\u queue.put((p,p.getPriority()))
self.parent.status\u box.Text+=“\n请进来。”
其他:
#查看等待名单
自助候车室附加(p)
self.parent.status\u box.Text+=“\n请在候诊室坐下。”
def患者(自身):
#检查候诊室->如果没有其他措施,则接收患者
如果len(自助候车室)>0:
p=self.waiting_room.pop()
自我补充患者(p)
def打印状态(自身):
#仅在有锁时调用
对于处于自我医院状态的p:
self.parent.status_box.Text+=p.getName()+“-Priority-”+p.getPriority()
#打印p.getName()+“-Priority-”+p.getPriority()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
form=MyForm()
管理员=管理员(表格)
form.admin=admin
Application.Run(表单)
其中,我自己的表单只是Form1,由python类MyForm继承。当我运行它时,我在C#中创建的表单会正确显示,但我找不到应该如何与控件交互。该表单显示为:

我想能够听到按钮按下,并从顶部的文本框阅读。状态将在较大的富文本框中打印。我非常感谢您的任何意见。提前谢谢

更新1:我认为我的问题的答案与所发现的类似。我已经将组件和方法的访问修饰符更改为public,现在它们出现了。如果我的问题解决了,我会更新这个

更新2:我可以看到这些com
import clr
import sys

clr.AddReference('System.Windows.Forms')
clr.AddReferenceToFileAndPath('C:/Full/Path/To/Compiled/FormProject.dll')

from System.Windows.Forms import *
from FormProject import *

class MyForm(Form1):
    def __init__(self):
        super(MyForm, self).__init__()
        self.input_button.Click += self.input_button_Click

    def input_button_Click(self, sender, e):
        self.output_box.Text += "Status: Tried to sign in with Name: \"" + self.input_box.Text + "\"\r\n"


Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)

form = MyForm()
Application.Run(form)