我正在开发自己的TextBox
课程。
SmallOffice.Windows.Forms
{
public class TextBox:System.Windows.Forms.TextBox
{
System.Windows.Forms.Button button;
public TextBox()
{
this.button = new System.Windows.Forms.Button();
this.Controls.Add(this.button);
}
}
}
问题是:如何为文本分配逻辑区域。例如,TextBox的大小将设置为Size(200, 20)
,文本的逻辑区域将设置为Rectangle(0, 0, 130, 20)
。
这样做TextBox中会有2个区域。左侧为文本,右侧为按钮。 这是必要的,以便文本不会与按钮重叠。
请给我一个线索,提示或任何有用的帮助。
答案 0 :(得分:2)
创建用户控件。使用2列1行的表格布局面板,将其停靠在父容器中,并将其背景颜色设置为白色。将用户控件的边框样式设置为3D。 取第一列中的文本框和第二列中的按钮。
您的用户控件设计器文件代码应该是:
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 544)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.MyPushButton = QtGui.QPushButton(self.centralwidget)
self.MyPushButton.setGeometry(QtCore.QRect(680, 40, 75, 23))
self.MyPushButton.setObjectName(_fromUtf8("MyPushButton"))
self.MyLabel = QtGui.QLabel(self.centralwidget)
self.MyLabel.setGeometry(QtCore.QRect(30, 30, 46, 13))
self.MyLabel.setObjectName(_fromUtf8("MyLabel"))
self.MyTextArea = QtGui.QTextBrowser(self.centralwidget)
self.MyTextArea.setGeometry(QtCore.QRect(20, 110, 721, 361))
self.MyTextArea.setObjectName(_fromUtf8("MyTextArea"))
self.MyTextField = QtGui.QLineEdit(self.centralwidget)
self.MyTextField.setGeometry(QtCore.QRect(100, 30, 571, 41))
self.MyTextField.setObjectName(_fromUtf8("MyTextField"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menuFIle = QtGui.QMenu(self.menubar)
self.menuFIle.setObjectName(_fromUtf8("menuFIle"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuFIle.menuAction())
self.MyPushButton.clicked.connect(self.download_click)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.MyPushButton.setText(_translate("MainWindow", "PushButton", None))
self.MyLabel.setText(_translate("MainWindow", "TextLabel", None))
self.menuFIle.setTitle(_translate("MainWindow", "FIle", None))
def download_click(self):
self.MyTextArea.textCursor().insertHtml('Im HERE!') # This is working as it should
url = str(self.MyTextField.text())
from Child_directory.some_other import childone
childone().somemethod(url)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
您也可以使用面板来满足需求。设置每个控件的停靠属性以确定正确的位置和大小。
在用户控件中获取和设置textbox define属性的文本:
namespace ProgressBar
{
partial class Tbox
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.panel2 = new System.Windows.Forms.Panel();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
this.tableLayoutPanel1.SuspendLayout();
this.panel2.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.tableLayoutPanel1);
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(275, 42);
this.panel1.TabIndex = 0;
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.BackColor = System.Drawing.SystemColors.ButtonHighlight;
this.tableLayoutPanel1.ColumnCount = 2;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 50F));
this.tableLayoutPanel1.Controls.Add(this.panel2, 0, 0);
this.tableLayoutPanel1.Controls.Add(this.button1, 1, 0);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 1;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(275, 42);
this.tableLayoutPanel1.TabIndex = 0;
//
// panel2
//
this.panel2.Controls.Add(this.textBox1);
this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel2.Location = new System.Drawing.Point(3, 3);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(219, 36);
this.panel2.TabIndex = 0;
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(219, 36);
this.textBox1.TabIndex = 0;
//
// button1
//
this.button1.Dock = System.Windows.Forms.DockStyle.Fill;
this.button1.Location = new System.Drawing.Point(228, 3);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(44, 36);
this.button1.TabIndex = 1;
this.button1.Text = "X";
this.button1.UseVisualStyleBackColor = true;
//
// Tbox
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.Controls.Add(this.panel1);
this.Name = "Tbox";
this.Size = new System.Drawing.Size(275, 42);
this.panel1.ResumeLayout(false);
this.tableLayoutPanel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
}
}
并在您的表单中访问它:
public string textBoxText { get { return textBox1.Text; } set { textBox1.Text = value; } }