会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132388个问题
JAVA 全系列/第十一阶段:分布式RPC调用和分布式文件存储/Dubbo 18136楼

源代码:

package com.bjsxt.TcpIp;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetDemo02 {
    public static void main(String[] args) throws UnknownHostException {

        InetAddress inetAddress1 = InetAddress.getByName("www.baidu.com");

        byte[] bytes1 = inetAddress1.getAddress();
        System.out.print("inetAddress1.getAddress(): ");
        for (byte b : bytes1) {
            System.out.print(b + ",");
        }
        System.out.println();
        System.out.println("inetAddress1.getHostAddress(): " + inetAddress1.getHostAddress());
        System.out.println("inetAddress1.getHostName(): " + inetAddress1.getHostName());
        System.out.println("=====================================");
        InetAddress inetAddress2 = InetAddress.getByAddress(bytes1);
        System.out.println("inetAddress2.getHostAddress(): " + inetAddress2.getHostAddress());
        System.out.println("inetAddress2.getHostName(): " + inetAddress2.getHostName());
        System.out.println("=====================================");
    }
}

运行结果:

inetAddress1.getAddress(): -73,-24,-25,-82,
inetAddress1.getHostAddress(): 183.232.231.174
inetAddress1.getHostName(): www.baidu.com
=====================================
inetAddress2.getHostAddress(): 183.232.231.174
inetAddress2.getHostName(): 183.232.231.174
=====================================

疑问:

为什么 inetAddress2.getHostName() 获取的不是“www.baidu.com”呢?

 inetAddress2是通过 getByAddress 获取的,用的就是 inetAddress1 的 getAddress 的返回值,我以为应该指向是相同的,实际运行获取的 getHostAddress() 相同,但是获取的 getHostName() 不同。

通过getByAddress(), 要填入什么参数,才能有 getHostName() 来获得 "www.baidu.com"呢?


JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 18137楼
WEB前端全系列/第二阶段:JavaScript编程模块/正则对象 18141楼
JAVA 全系列/第十一阶段:分布式RPC调用和分布式文件存储/Dubbo 18143楼
JAVA 全系列/第一阶段:JAVA 快速入门/控制语句、方法、递归算法 18144楼
Python 全系列/第十阶段:Flask百战电商后台项目/Flask百战电商后台项目 18146楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 18148楼

#Text的测试

from tkinter import *
import webbrowser


class Application(Frame):
    def __init__(self,Master=None):
        super().__init__(Master)
        self.Master=Master
        self.pack()
        self.creatWidget()


    def creatWidget(self):
        self.w1 = Text(root, width=40,height=40, bg="blue")
        #宽度为20个字母,10个汉字,高度12行高
        self.w1.pack()

        self.w1.insert(1.0, "0123456789\nabcdefg")
        self.w1.insert(2.3, "锄禾日当午,汗滴禾下土。谁知盘中餐,粒粒皆辛苦\n")

        Button(self,text="重复插入文本",command=self.insertText).pack(side="left")
        Button(self, text="返回文本", command=self.returnText).pack(side="left")
        Button(self, text="添加图片", command=self.addImage).pack(side="left")
        Button(self, text="添加组件", command=self.addWidget).pack(side="left")
        Button(self, text="通过tag精确控制文本", command=self.testTag).pack(side="left")

    def insertText(self):
        # INSERT索引表示再光标处插入
        self.w1.insert(INSERT, "钟辉")
        #END索引表示在最后面插入
        self.w1.insert(END, "加油坚持努力")
        self.w1.insert(1.8,"ahui")

    def returnText(self):
        # Indexes(索引)是用来指向Text组件中文本的位置,Text的组件索引也是对应实际字符之间的位置。
        # 核心:行号以1开始 列号以0开始
        print(self.w1.get(1.0,1.6))
        self.w1.insert(1.8,"zh")
        print("所有文本内容:\n",self.w1.get(1.0,END))

    def addImage(self):
        global photo
        photo=PhotoImage(file = r"E:\new start\gui\imgs\littlecat.gif")
        self.w1.image_create(END, image=photo)

    def addWidget(self):
        b1 = Button(self.w1, text="努力学")
        self.w1.window_create(INSERT, window=b1)

    def testTag(self):
        self.w1.delete(1.0,END)
        self.w1.insert(INSERT, "钟辉要选好Python\n百度")
        self.w1.tag_add("zh",1.0,2.0)
        self.w1.tag_config("zh",background="yellow")

        self.w1.tag_add("baidu", 2.0, 2.2)
        self.w1.tag_config("baidu", underline=True)
        self.w1.tag_bind("baidu", "<Button-1>", self.webshow)

    def webshow(self,event):
        webbrowser.open("http://www.baidu.com")


if __name__=="__main__":
    root = Tk()
    root.geometry("400x400")
    app = Application(Master=root)
    root.mainloop()

问题:

  1. 为什么创建Text时后面接的时root而不是self呢,而创建Button时后面却接self,是不是说Frame没有办法管到Text呢

  2. tag_config中的background不能用缩写bg,而text后直接加属性的话是可以用缩写的,这是说config中是只能用全写吗

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

课程分类

百战程序员微信公众号

百战程序员微信小程序

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