会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133940个问题
人工智能/第七阶段:机器学习-无监督学习(旧)/EM算法和GMM高斯混合模型 24226楼
JAVA 全系列/第三十阶段:高并发实战和BATJ大厂面试重难点/Java并发编程核心 24227楼

'''测试一个经典的GUI程序的写法,使用面向对象的方法'''


from tkinter import *

from tkinter import messagebox


class Application(Frame):

    '''一个经典的GUI程序的类的写法'''


    def __init__(self,master=None):

        super().__init__(master)    #super()代表的是父类的定义,而不失父类的对象   super()是调用父类的方法,这里是调用父类初始化方法

        self.master=master

        self.pack()


        self.createWidget()


    def createWidget(self):

        '''创建组件'''

        self.btn01=Button(self)

        self.btn01['text']='点击送花'

        self.btn01.pack()   #调用布局管理器,把组建对象合理的放到窗口里

        self.btn01['command']=self.songhua   #command为调用函数


        #创建一个退出按钮

        self.btnQuit=Button(self,text='退出',command=root.destroy)      #destroy是结束窗口

        self.btnQuit.pack()


    def songhua(self):

        messagebox.showinfo('送花','送你99朵玫瑰花,亲亲我吧')


if __name__=='__main__':

    root=Tk()

    root.geometry('400x100+200+300')

    root.title('一个经典的GUI程序的类的测试')

    app=Application(master=root)

    root.mainloop()    #调用组件的mainloop方法,进入事件循环,展示效果



'''

老师您好,以下遇到几个问题:

1.self.btn01=Button(self)和self.btnQuit=Button(self,text='退出',command=root.destroy) 为什么只把self传进去里面呢,看得不是很懂?

2.from tkinter import messagebox  在tkinter中导入的这个messagebox这个是啥玩意呢?

3.if __name__=='__main__':  这个测试代码不是只有在程序脚本直接运行的时候才会有效的吗,怎么在模块导入的情况下也可以正常执行?

4.app=Application(master=root) 为什么要在前面赋值给app,不加可以嘛?

5.self.master=master  这一行我注释掉程序也一样可以正常执行,这是为什么呢?


'''


Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 24228楼

老师 ,我照着高淇老师上传的模块显示,上传失败是怎么回事

1、Microsoft Windows [版本 10.0.18363.1379]
(c) 2019 Microsoft Corporation。保留所有权利。

(mypro-modules) C:\Users\lzh\Desktop\资料\mypro-modules\math3>python setup.py sdist upload
running sdist
running check
warning: check: missing required meta-data: url

warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list)

warning: sdist: standard file not found: should have one of README, README.txt, README.rst

writing manifest file 'MANIFEST'
creating baizhanSuperMath-1.0
creating baizhanSuperMath-1.0\baizhanSuperMath
making hard links in baizhanSuperMath-1.0...
hard linking setup.py -> baizhanSuperMath-1.0
hard linking baizhanSuperMath\__init__.py -> baizhanSuperMath-1.0\baizhanSuperMath
hard linking baizhanSuperMath\demo1.py -> baizhanSuperMath-1.0\baizhanSuperMath
hard linking baizhanSuperMath\demo2.py -> baizhanSuperMath-1.0\baizhanSuperMath
Creating tar archive
removing 'baizhanSuperMath-1.0' (and everything under it)
running upload
Submitting dist\baizhanSuperMath-1.0.tar.gz to https://upload.pypi.org/legacy/
Upload failed (400): Invalid value for blake2_256_digest. Error: Use a valid, hex-encoded, BLAKE2 message digest.
error: Upload failed (400): Invalid value for blake2_256_digest. Error: Use a valid, hex-encoded, BLAKE2 message digest.

(mypro-modules) C:\Users\lzh\Desktop\资料\mypro-modules\math3>

就显示失败 什么原因啊

image.png

Python 全系列/第二阶段:Python 深入与提高/模块 24229楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 24230楼
JAVA 全系列/第一阶段:AI驱动的JAVA编程/变量、数据类型、运算符 24231楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 24232楼
JAVA 全系列/第十阶段:权限控制与安全认证/Spring Security(旧) 24233楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 24234楼
Python 全系列/第六阶段:数据库与AI协同技术实战/mysql介绍与环境安装 24236楼
JAVA 全系列/第十阶段:权限控制与安全认证/Spring Security(旧) 24237楼
JAVA 全系列/第九阶段:Spring Boot实战/Spring Boot 24238楼
JAVA 全系列/第六阶段:JavaWeb开发/Web实战案例 24239楼

package com.cj;
//客户端

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

public class BasicSocketClient {
    public static void main(String[] args) {
        Socket socket = null;
        PrintWriter pw = null;
        //创建Socket对象:两个参数1.服务端IP地址2.服务端所监听的端口
        try {
            socket = new Socket("192.168.43.57", 8888);
            pw = new PrintWriter(socket.getOutputStream());
            pw.println("服务端你好!!!");
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (pw != null) {
                pw.close();
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
public class BasicSocketServer {
    public static void main(String[] args) {
        Socket socket=null;
        BufferedReader br=null;
        try {
            ServerSocket serverSocket=new ServerSocket(8888);
            System.out.println("服务器启动等待监听。。。。。");
            //开启监听
            socket=serverSocket.accept();
            //读取客服端发送的消息
            br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            System.out.println(br.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

问题:服务端启动后,正常打印输出

客户端启动后,程序直接结束,不会输出“服务端你好”

image.png

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 24240楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637