在一个主窗口中,点击一个链接, 弹出一个子窗口 , 父窗口保留在子窗口中点击关闭, 关闭子窗口. 子窗口的位置位于屏幕的中间
时间: 2023-05-21 17:01:39 浏览: 66
可以使用 JavaScript 来实现这个功能,以下是一个简单的实现代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>弹出子窗口</title>
<script type="text/javascript">
function openChildWindow() {
var childWindow = window.open("child.html", "childWindow", "width=400,height=300,top=" + (screen.height/2-150) + ",left=" + (screen.width/2-200));
childWindow.focus();
}
</script>
</head>
<body>
<a href="#" onclick="openChildWindow(); return false;">点击打开子窗口</a>
</body>
</html>
```
在这个代码中,我们定义了一个 `openChildWindow` 函数,当用户点击链接时,调用这个函数来打开子窗口。子窗口的 URL 是 `child.html`,窗口的名称是 `childWindow`,窗口的大小是 400x300,位置位于屏幕的中间。当用户关闭子窗口时,窗口会自动关闭。
相关问题
python tkinter点击按钮弹出另一个窗口,然后再关闭子窗口
好的,以下是一个示例代码,演示如何在 Python tkinter 中点击按钮弹出另一个窗口,然后再关闭子窗口。
```
import tkinter as tk
def open_window():
# 创建子窗口
sub_window = tk.Toplevel(root)
sub_window.title("子窗口")
sub_window.geometry("200x100")
# 添加标签和按钮
label = tk.Label(sub_window, text="这是一个子窗口")
label.pack(pady=10)
button = tk.Button(sub_window, text="关闭子窗口", command=sub_window.destroy)
button.pack()
root = tk.Tk()
root.title("主窗口")
root.geometry("200x100")
# 添加按钮
button = tk.Button(root, text="弹出子窗口", command=open_window)
button.pack(pady=10)
root.mainloop()
```
在这个例子中,我们创建了一个名为 `open_window` 的函数,用来创建子窗口。在主窗口中添加了一个按钮,点击按钮会调用 `open_window` 函数并弹出子窗口。子窗口中添加了一个标签和一个按钮,点击按钮会调用 `sub_window.destroy` 函数关闭子窗口。
注意,子窗口是通过 `Toplevel` 函数创建的,它是一个独立的窗口,可以单独设置大小、标题等属性。关闭子窗口可以调用 `destroy()` 方法。
C++Qt实现,点击主窗口的Toolbutton,弹出子窗口,在子窗口中设置一个中心窗口
可以按照以下步骤实现:
1. 在Qt Creator中创建一个主窗口工程,添加一个ToolButton控件。
2. 在主窗口的.cpp文件中,连接ToolButton的clicked()信号到一个槽函数。
3. 在槽函数中创建一个子窗口,并设置其为主窗口的子窗口。
4. 在子窗口中添加一个QWidget控件,并设置其为中心窗口。
以下是示例代码:
```cpp
// 主窗口.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "subwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->toolButton, &QToolButton::clicked, this, &MainWindow::onToolButtonClicked);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onToolButtonClicked()
{
SubWindow *subWindow = new SubWindow(this);
setCentralWidget(subWindow);
}
```
```cpp
// 子窗口.cpp
#include "subwindow.h"
#include "ui_subwindow.h"
SubWindow::SubWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::SubWindow)
{
ui->setupUi(this);
QWidget *centerWidget = new QWidget(this);
setCentralWidget(centerWidget);
}
SubWindow::~SubWindow()
{
delete ui;
}
```
注意,子窗口需要使用QMainWindow作为基类,并且要在构造函数中调用setCentralWidget()方法来设置中心窗口。在主窗口中,点击ToolButton后,需要创建一个子窗口并调用setCentralWidget()方法来设置主窗口的中心窗口。