如题。用radioButton替换了之前的在文本框手动输入邮寄地址的方式。关于radioButton的属性,使用方法依然还需摸索。

from PySide2.QtWidgets import QApplication, QMessageBox, QRadioButton, QButtonGroup
from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import QFile

class Stats:

    def __init__(self):
        # 从文件中加载UI定义
        #qfile_stats = QFile('1.ui')
        #qfile_stats.open(QFile.ReadOnly)
        #qfile_stats.close()

        # 从 UI 定义中动态 创建一个相应的窗口对象
        # 注意:里面的控件对象也成为窗口对象的属性了
        # 比如 self.ui.button , self.ui.textEdit

        #self.ui = QUiLoader().load(qfile_stats)
        self.ui = QUiLoader().load('3.ui')

        self.ui.pushButton.clicked.connect(self.handleCalc)

        self.ui.buttongroup = QButtonGroup()                        #定义一个按钮组
        self.ui.buttongroup.addButton(self.ui.radioButton)          #把这几个 radiobutton 加入到按钮组中 
        self.ui.buttongroup.addButton(self.ui.radioButton2)
        self.ui.buttongroup.addButton(self.ui.radioButton3)
        self.ui.buttongroup.addButton(self.ui.radioButton4)
        #self.ui.buttongroup.buttonClicked.connect(self.pressbutton)    #可以自定义按钮组内的按钮被点击后的执行函数


    # def pressbutton(self):                                        #按钮组内的按钮被点击时执行的函数

    #     x = self.ui.buttongroup.checkedButton().text()

    #     print(x)

    def handleCalc(self):

        x = self.ui.buttongroup.checkedButton().text()              #获取被 check 的 radiobutton 的名称,此为关键一步,作为后续计算总价的分类依据
                                                                    #checkedButton().text(), 记住这个函数
        weight = float(self.ui.textEdit1.toPlainText())
        price = ''

        if x == '东三省/宁夏/青海/海南':

            if 0 < weight <= 3:

                price = '总价是12元'

            elif weight > 3:
                
                price = f'总价是{round(12+(weight-3)*10,2)}元'

            else:

                price = '重量错误!'

        elif x == '新疆/西藏':

            if 0 < weight <= 3:

                price = '总价是20元'

            elif weight > 3:
                
                price = f'总价是{round(20+(weight-3)*20,2)}元'

            else:

                price = '重量错误!'

        elif x == '港澳台/国外':

            if 0 < weight <= 3:

                price = '不接受寄件'

            elif weight > 3:
                
                price = '请联系总公司'

            else:

                price = '重量错误!'

        elif x == '其他地区':

            if 0 < weight <= 3:

                price = '总价是10元'
            
            elif weight >3:

                price = f'总价是{round(10+(weight-3) * 5,2)}元'

            else:

                price = '重量错误!'

        QMessageBox.about(self.ui,
                    '计算结果',
                    f'''您好,\n{price}'''
                    )



     
app = QApplication([])
stats = Stats()
stats.ui.show()
app.exec_()

2020/9/20 更新:添加了一个文本框,用于显示计算结果。比之 messagebox,是另一种较为常用的显示方式,日后某些场景可能还会使用到,在这里就当是提前练习了。更新后的代码如下。

from PySide2.QtWidgets import QApplication, QMessageBox, QRadioButton, QButtonGroup
from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import QFile

class Stats:

    def __init__(self):
        # 从文件中加载UI定义
        #qfile_stats = QFile('1.ui')
        #qfile_stats.open(QFile.ReadOnly)
        #qfile_stats.close()

        # 从 UI 定义中动态 创建一个相应的窗口对象
        # 注意:里面的控件对象也成为窗口对象的属性了
        # 比如 self.ui.button , self.ui.textEdit

        #self.ui = QUiLoader().load(qfile_stats)
        self.ui = QUiLoader().load('3.ui')

        self.ui.pushButton.clicked.connect(self.handleCalc)

        self.ui.buttongroup = QButtonGroup()                        #定义一个按钮组
        self.ui.buttongroup.addButton(self.ui.radioButton)          #把这几个 radiobutton 加入到按钮组中 
        self.ui.buttongroup.addButton(self.ui.radioButton2)
        self.ui.buttongroup.addButton(self.ui.radioButton3)
        self.ui.buttongroup.addButton(self.ui.radioButton4)
        #self.ui.buttongroup.buttonClicked.connect(self.pressbutton)    #可以自定义按钮组内的按钮被点击后的执行函数,此案例中不需要


    # def pressbutton(self):                                        #按钮组内的按钮被点击时执行的函数

    #     x = self.ui.buttongroup.checkedButton().text()

    #     print(x)

    def handleCalc(self):

        x = self.ui.buttongroup.checkedButton().text()              #获取被 check 的 radiobutton 的名称,此为关键一步,作为后续计算总价的分类依据
                                                                    #checkedButton().text(), 记住这个函数
        w = self.ui.textEdit1.toPlainText()                                                            
        
        price = ''

        if w != '' :

            weight = float(w)

            if x == '东三省/宁夏/青海/海南':

                if 0 < weight <= 3:

                    price = '总价是12元'

                elif weight > 3:
                    
                    price = f'总价是{round(12+(weight-3)*10,2)}元'

                else:

                    price = '重量错误!'

            elif x == '新疆/西藏':

                if 0 < weight <= 3:

                    price = '总价是20元'

                elif weight > 3:
                    
                    price = f'总价是{round(20+(weight-3)*20,2)}元'

                else:

                    price = '重量错误!'

            elif x == '港澳台/国外':

                if 0 < weight <= 3:

                    price = '不接受寄件'

                elif weight > 3:
                    
                    price = '请联系总公司'

                else:

                    price = '重量错误!'

            elif x == '其他地区':

                if 0 < weight <= 3:

                    price = '总价是10元'
                
                elif weight >3:

                    price = f'总价是{round(10+(weight-3) * 5,2)}元'

                else:

                    price = '重量错误!'

            self.ui.textEdit1.clear()
            self.ui.textEdit2.clear()
            self.ui.textEdit2.insertPlainText(price)                    #在文本框内输出(插入)计算结果

            # QMessageBox.about(self.ui,
            #             '计算结果',
            #             f'''您好,\n{price}'''
            #             )
        else:

            self.ui.textEdit2.clear()


     
app = QApplication([])
stats = Stats()
stats.ui.show()
app.exec_()