Zero_qll 2023-04-27 18:48 采纳率: 100%
浏览 18
已结题

python无法引用另一个文件中的列表

我需要在main.py里从window.py中获取列表text_content的值,但是失败了,有没有大佬知道为什么,代码如下


```python

mian.py

import window
from window import text_content

def main():
    window.mainWin()
    content = text_content
    print(content)

main()

window.py

from tkinter import *
from  tkinter import scrolledtext

text_content = []

class Application(Frame):

    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.grid()
        self.createWidget()

    def createWidget(self):
        
        self.la2 = Label(self, text='重量(kg):').grid(row=1, column=0, padx=(10, 0))
        self.en2 = Entry(self)
        self.en2.grid(row=1, column=1, columnspan=1, padx=(0, 10), ipadx=20)  # 1行1列,跨2列

        self.btn = Button(self, text="确定", command=self.click_btn).grid(row=1, column=2, padx=(0, 30), ipadx=30)
        self.btn2 = Button(self, text="输入完毕", command=self.click_btn2).grid(row=4, column=2, padx=(0, 30), ipadx=30)
       
        self.text = scrolledtext.ScrolledText(self, width=50, height=20)
        self.text.grid(row=3, column=0, columnspan=3, padx=5, pady=10)

    def click_btn(self):
       
        input2 = self.en2.get()
        str =  input2 + ' '

        #print(getinputstr)
        print(input1, input2)
        self.text.insert('1.0', str+'\n')
        self.en2.delete(0, END)

    def click_btn2(self):
        text_content = (self.text.get('0.0', 'end').replace('\n', ' ')).split()
        print(text_content)
        #return text_content

def mainWin():
    root = Tk()
    root.title('NuContent')
 
    sw = root.winfo_screenwidth()
    sh = root.winfo_screenheight()
    ww = 410
    wh = 400
    x = (sw - ww)/2
    y = (sh - wh)/3
    root.geometry("%dx%d+%d+%d" % (ww, wh, x, y))
    root.resizable(False, False)
    app = Application(master=root)

    app.mainloop()

```

  • 写回答

3条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-04-27 23:00
    关注
    • 这篇博客: 解决python爬虫乱码问题及.text与.content的区别中的 遇到问题 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
    • 爬取某网站(不便透露)的帖子过程中,发现数据解析不对,中文乱码,或者只能解析出一半的中文。
      开发者模式查看到charset="gbk"
      然而该网页的编码方式如下:

      response = requests.get(test_url)
      
      print(response.encoding) #查看网页返回的字符集类型
      print(response.apparent_encoding) #自动判断字符集类型
      

      输出为:

      ISO-8859-1
      GB2312

      enconding是从http从中猜测的响应内容编码方式,若header中没有charset字段则默认为ISO-8859-1编码模式,则无法解析中文。
      apparent——encoding是从内容中分析出的响应内容编码方式。

      因此,requess模块猜测网页的编码方式与实际的编码方式不同,网页申明的编码方式是gbk。

      尝试输出print(response.text[:1000]),结果如下
      在这里插入图片描述

      再尝试输出print(response.text[:1000])结果如下:
      在这里插入图片描述

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月12日
  • 已采纳回答 5月4日
  • 创建了问题 4月27日

悬赏问题

  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路
  • ¥15 内网办公电脑进行向日葵
  • ¥15 如何输入双曲线的参数a然后画出双曲线?我输入处理函数加上后就没有用了,不知道怎么回事去掉后双曲线可以画出来