1.执行环境判断 window 或 self

window or self ?

在 underscore 的判断所处环境的代码中,似乎我们没有看到 window 对象的引用,其实,在浏览器环境下,self 保存的就是当前 window 对象的引用。那么相比较于使用 window,使用 self 有什么优势呢?我们看到 MDN 上有这么一句话:

The Window.self read-only property returns the window itself, as a WindowProxy. It can be used with dot notation on a window object (that is, window.self) or standalone (self). The advantage of the standalone notation is that a similar notation exists for non-window contexts, such as in Web Workers.

概括来说,就是 self 还能用于一些不具有窗口的上下文环境中,比如 Web Workers。所以,为了服务于更多场景,underscore 选择了更加通用的 self 对象。

!(function(){
    var root = (typeof self == "object" && self.self === self && self) || (typeof global == "object" && global.global === global && global) || this;
});

  

转载于:https://www.cnblogs.com/alantao/p/7831287.html

def edit_data(self): # 创建一个新窗口,并设置其属性 new_window = QWidget() new_window.status_label1 = QLabel() new_window.status_label2 = QLabel() new_window.setWindowTitle("修改员工信息") new_window.setGeometry(200, 200, 400, 300) # 创建返回按钮并设置其属性 return_button = QPushButton("返回", new_window) return_button.clicked.connect(new_window.close) return_button.clicked.connect(self.window.show) # 设置输入框 self.input_box1 = QLineEdit(new_window) new_window.status_label1.setText("输入修改前的对象,格式为:部门/姓名/性别/职位/工号/状态,对象") self.input_box2 = QLineEdit(new_window) new_window.status_label2.setText("输入修改后的对象,格式为:部门/姓名/性别/职位/工号/状态,对象") # 设置修改按钮 edit_button = QPushButton("修改",new_window) edit_button.clicked.connect(lambda: self.edit(new_window)) # 添加表格、按键到新窗口 self.v_layout = QVBoxLayout(new_window) self.v_layout.addWidget(new_window.status_label1) self.v_layout.addWidget(self.input_box1) self.v_layout.addWidget(new_window.status_label2) self.v_layout.addWidget(self.input_box2) self.v_layout.addWidget(edit_button) self.v_layout.addWidget(return_button) new_window.setLayout(self.v_layout) new_window.show() def edit(self,new_window): # 获取输入框的数据 data1 = self.input_box1.text().split(",") if len(data1) != 2: new_window.status_label1.setText("输入格式错误!请按照 部门/姓名/性别/职位/工号/状态,对象 的格式输入。") return data2 = self.input_box2.text().split(",") if len(data2) != 2: new_window.status_label1.setText("输入格式错误!请按照 部门/姓名/性别/职位/工号/状态,对象 的格式输入。") return # 执行 SQL 修改语句 sql = "UPDATE Staff SET ? = ? WHERE ? = ?" try: self.cursor.execute(sql, data1[0], data1[1], data2[0], data2[1]) self.cnxn.commit() new_window.status_label1.setText("数据修改成功!") except: new_window.status_label1.setText("数据修改失败!")无法修改数据,在提示正确修改后数据并没有修改
06-02
import os from PyQt5.QtCore import Qt from PyQt5.QtGui import QPixmap, QIcon from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QHBoxLayout, QTreeView, QFileSystemModel class ImageViewer(QWidget): def init(self, folder_path): super().init() self.folder_path = folder_path self.image_dict = {} self.current_image = None self.setWindowTitle("Image Viewer") self.setFixedSize(1000, 600) self.image_label = QLabel(self) self.image_label.setAlignment(Qt.AlignCenter) self.tree_view = QTreeView() self.tree_view.setMinimumWidth(250) self.tree_view.setMaximumWidth(250) self.model = QFileSystemModel() self.model.setRootPath(folder_path) self.tree_view.setModel(self.model) self.tree_view.setRootIndex(self.model.index(folder_path)) self.tree_view.setHeaderHidden(True) self.tree_view.setColumnHidden(1, True) self.tree_view.setColumnHidden(2, True) self.tree_view.setColumnHidden(3, True) self.tree_view.doubleClicked.connect(self.tree_item_double_clicked) self.main_layout = QHBoxLayout(self) self.main_layout.addWidget(self.tree_view) self.main_layout.addWidget(self.image_label) self.load_images() self.update_image() def load_images(self): for file_name in os.listdir(self.folder_path): if file_name.lower().endswith((".jpg", ".jpeg", ".png", ".gif", ".bmp")): file_path = os.path.join(self.folder_path, file_name) self.image_dict[file_name] = file_path current_image = list(self.image_dict.keys())[0] def update_image(self): if self.current_image is not None: pixmap = QPixmap(self.image_dict[self.current_image]) self.image_label.setPixmap(pixmap.scaled(self.width() - self.tree_view.width(), self.height(), Qt.KeepAspectRatio, Qt.SmoothTransformation)) def tree_item_double_clicked(self, index): file_name = self.model.fileName(index) if file_name in self.image_dict: self.current_image = file_name self.update_image() def keyPressEvent(self, event): if event.key() == Qt.Key_A: self.previous_image() elif event.key() == Qt.Key_D: self.next_image() elif event.key() in [Qt.Key_1, Qt.Key_2, Qt.Key_3, Qt.Key_4, Qt.Key_5]: self.save_text_file(event.key() - Qt.Key_0) def previous_image(self): if self.current_image is not None: file_names = list(self.image_dict.keys()) current_index = file_names.index(self.current_image) if current_index > 0: self.current_image = file_names[current_index - 1] else: self.current_image = file_names[-1] self.update_image() def next_image(self): if self.current_image is not None: file_names = list(self.image_dict.keys()) current_index = file_names.index(self.current_image) if current_index < len(file_names) - 1: self.current_image = file_names[current_index + 1] else: self.current_image = file_names[0] self.update_image() def save_text_file(self, number): if self.current_image is not None: file_name = self.current_image txt_file_path = os.path.join(self.folder_path, os.path.splitext(file_name)[0] + ".txt") with open(txt_file_path, "w") as file: file.write(str(number)) if name == "main": import sys app = QApplication(sys.argv) viewer = ImageViewer("D:/图片/wallpaper") viewer.show() sys.exit(app.exec_())这份代码实现不了使用键盘的A键向上翻页以及D键向下翻页,也实现不了键盘数字键生成相应txt文档,帮我分析一下错在哪里
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值