使用python类作为要启动的对象

2024-09-29 01:34:18 发布

您现在位置:Python中文网/ 问答频道 /正文

不熟悉OOP和Python,而且是个老屁,对c中的过程编码很在行

无论如何,我已经创建了一个Python类; 它为用户显示一个带有一些选项的窗口。 我从网上复制了一个脚本(helloworld脚本),并学会了如何修改它来做我想做的事情。你知道吗

我想创建另一个类,当用户在第一个类中选择特定选项时,该类的工作方式与第一个类(克隆)的工作方式非常相似。你知道吗

它应该在另一个窗口中设置一组选项。 我改了不同的名字,我想应该和第一个不同。你知道吗

所以,我的问题是,如果用户在第一个类中做出了适当的选择,如何使用第二个类作为对象并从第一个类中“调用”它。你知道吗

我在第一个类中包含了第二个类的import语句,但不知道当用户选择调用第二个类时应该在代码中插入什么。你知道吗

添加代码示例(经过适当编辑,但可能不够):

第一个文件:

import Tkinter as tk
import sys
import Example_Edit
from PIL import Image, ImageTk
from Tkinter import Tk, Label, BOTH
from ttk import Frame, Style

class Main_Loader(tk.Frame):

    def __init__(self, master):
        # Initialize window using the parent's constructor
        tk.Frame.__init__(self,
                          master,
                          width=400,
                          height=300)
        # Set the title
        self.master.title('Main Data Loader')

        # This allows the size specification to take effect
        self.pack_propagate(0)

        # We'll use the flexible pack layout manager
        self.pack()

        # The option selector
        # Use a StringVar to access the selector's value
        self.option_var = tk.StringVar()
        self.option = tk.OptionMenu(self,
                                      self.option_var,
                                      '(Select a Workflow)',
                                      'EDIT Project Data',
                                      'Load SEGY data to IDS',
                                      'Preview a SEGY line',
                                      'Move SEGY file to DBFS'
                                      )
        self.option_var.set('(Select a Workflow)')

#more code to complete the frame



    def do_something(self):

        Edit = Example_Edit.Main_Edit

        if self.option_var.get() == '(Select a Workflow)':
            print('Select a Workflow')
        elif self.option_var.get() == 'EDIT Project Data':
            print('Do--%s' % (self.option_var.get()))
            Edit(self)    :'<===============what goes here===================='
        elif self.option_var.get() == 'Move SEGY file to DBFS':
            print('Do--%s' % (self.option_var.get()))
        elif self.option_var.get() == 'Preview a SEGY line':
            print('Do--%s' % (self.option_var.get()))
        elif self.option_var.get() == 'Load SEGY data to IDS':
            print('Do--%s' % (self.option_var.get()))

    def end_it(self):
        quit() 

    def run(self):
        ''' Run the app '''
        self.mainloop()

app = Main_Loader(tk.Tk())
app.run()

第二个文件:

import Tkinter as tk
import sys
from PIL import Image, ImageTk
from Tkinter import Tk, Label, BOTH
from ttk import Frame, Style

class Main_Edit(tk.Frame):

    def __init__(self, master):

        # Initialize window using the parent's constructor

        tk.Frame.__init__(self,
                          master,
                          width=400,
                          height=300)
        # Set the title
        self.master.title('Main Netadata Editor')



#more code here to complete the frame


    def do_something(self):

#       do stuff

    def end_it(self):
        quit() 

好吧,这就是我要做的。温柔点,别笑,我是个新手。你知道吗


Tags: thetofromimportselfmastergetmain
3条回答

没有.py的另一个文件的文件名,然后是句点,然后是类名。你知道吗

因此,例如,如果文件名为fileA.pyfileB.py,并且分别包含类ClassAClassB,则可以从fileA.py访问ClassB,如下所示:

import fileB

instanceB = fileB.ClassB()

再做一个回答,避免混淆。你知道吗

这条线:

Edit = Example_Edit.Main_Edit

应该是

Edit = Example_Edit.Main_Edit(self)

在这里,您将创建一个名为Edit的nameplace,它是主编辑类的一个实例,在示例\u Edit文件中,因为主编辑接受一个参数,一个类实例(您称之为master),您将它一起传递(“self”)。你知道吗

下一步:

Edit(self)  '<===============what goes here===================='

您可以替换它并开始使用编辑方法[从主编辑类]。你知道吗

简单地说:

elif self.option_var.get() == 'EDIT Project Data':
    Edit.do_something()
elif self.option_var.get() == 'end':
    Edit.end_it()

编辑:

顺便说一下:

在你的主修课上

这条线:

self.master.title('Main Netadata Editor')

应该是:

self.master.master.title('Main Netadata Editor')

因为主类Main\u Loader也使用主名称place。你知道吗

这就是为什么我在另一个答案中说,在类实例中使用类实例很麻烦。你知道吗

我想你要找的是第一类的一个子类。你知道吗

像这样:

class myClass():
    def __init__(self,value,text):
        self.value = value
        self.text = text

    def show_values(self):
        print self.value,self.text

class myCloneClass(myClass):
    def show_values(self):
        print "Value:",self.value
        print "Text:",self.text

a = myClass(1,'Hello')
b = myCloneClass(2,'World')
a.show_values()
b.show_values()

输出:

1 Hello
Value: 2
Text: World

I want to create another class that works very similarly to the first (a clone)

myClass有一个方法可以打印它的值,但格式不正确, myCloneClass有相同的方法,用相同的名称调用,以更好的方式打印其值


how to use the second class as an object and "call" it from the first if the user makes the appropriate selection in the first class

或者您正在另一个类对象中查找类对象

像这样:

class myFirstClass():
    def __init__(self):
        self.color = raw_input("What is favorite color? ")
        print "Your favorite color is",self.color
        if self.color == 'blue':
            self.sub_instance = mySecondClass()

class mySecondClass():
    def __init__(self):
        self.food = raw_input("What is favorite food? ")
        print "Your favorite food is", self.food

a = myFirstClass()

“a”是myFirstClass的一个实例,如果用户选择“blue”作为最喜欢的颜色,它将创建一个名为self.sub\u实例,它是mySecondClass的一个实例。我不知道为什么您会想要这样的东西,因为您可以使用第一个类来存储新的值。这是一种相当麻烦的处理方式。你知道吗

要获取您喜欢的食物,您需要使用eihter:

a.sub_instance.food # outside the myFirstClass
self.sub_instance.food # inside the myFirstClass

但如果最喜欢的颜色不是“蓝色”,它就会失败,因为它会创建新的nameplace(self.sub\u实例),引发AttributeError。你知道吗

相关问题 更多 >