<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 對話框 > 原文: [http://zetcode.com/tutorials/ironpythontutorial/dialogs/](http://zetcode.com/tutorials/ironpythontutorial/dialogs/) 在 IronPython Mono Winforms 教程的這一部分中,我們將討論對話框。 對話框窗口或對話框是大多數現代 GUI 應用必不可少的部分。 對話被定義為兩個或更多人之間的對話。 在計算機應用中,對話框是一個窗口,用于與應用“對話”。 對話框用于輸入數據,修改數據,更改應用設置等。對話框是用戶與計算機程序之間進行通信的重要手段。 基本上有兩種類型的對話框。 預定義對話框和自定義對話框。 ## `FolderBrowserDialog` 此對話框提示用戶選擇一個文件夾。 `folderbrowserdialog.py` ```py #!/usr/bin/ipy import clr clr.AddReference("System.Windows.Forms") clr.AddReference("System.Drawing") from System.Windows.Forms import Application, Form, StatusBar from System.Windows.Forms import ToolBar, ToolBarButton, FolderBrowserDialog from System.Windows.Forms import DialogResult class IForm(Form): def __init__(self): self.Text = "FolderBrowserDialog" toolbar = ToolBar() toolbar.Parent = self openb = ToolBarButton() self.statusbar = StatusBar() self.statusbar.Parent = self toolbar.Buttons.Add(openb) toolbar.ButtonClick += self.OnClicked self.CenterToScreen() def OnClicked(self, sender, event): dialog = FolderBrowserDialog() if (dialog.ShowDialog(self) == DialogResult.OK): self.statusbar.Text = dialog.SelectedPath Application.Run(IForm()) ``` 我們有一個工具欄和一個工具欄按鈕。 點擊按鈕,`FolderBrowserDialog`出現在屏幕上。 所選文件夾的名稱顯示在狀態欄中。 ```py dialog = FolderBrowserDialog() ``` `FolderBrowserDialog`已創建。 ```py if (dialog.ShowDialog(self) == DialogResult.OK): self.statusbar.Text = dialog.SelectedPath ``` `ShowDialog()`方法在屏幕上顯示對話框。 如果單擊對話框的“確定”按鈕,則所選的目錄路徑將顯示在狀態欄上。 ![FolderBrowserDialog](https://img.kancloud.cn/c8/2b/c82ba68cff4c1aa5c9a248774625db9a_330x352.jpg) 圖:`FolderBrowserDialog` ## `ColorDialog` 該對話框顯示可用的顏色以及使用戶能夠定義自定義顏色的控件。 `colordialog.py` ```py #!/usr/bin/ipy import sys import clr clr.AddReference("System.Windows.Forms") clr.AddReference("System.Drawing") from System.Windows.Forms import Application, Form, ToolBar from System.Windows.Forms import ToolBarButton, ControlStyles, ColorDialog from System.Windows.Forms import DialogResult from System.Drawing import Size, Color, SolidBrush, Rectangle RECT_WIDTH = 100 RECT_HEIGHT = 100 class IForm(Form): def __init__(self): self.Text = "ColorDialog" self.color = Color.Blue toolbar = ToolBar() toolbar.Parent = self openb = ToolBarButton() toolbar.Buttons.Add(openb) toolbar.ButtonClick += self.OnClicked self.LocateRect() self.SetStyle(ControlStyles.ResizeRedraw, True) self.Paint += self.OnPaint self.CenterToScreen() def OnPaint(self, event): g = event.Graphics self.LocateRect() brush = SolidBrush(self.color) g.FillRectangle(brush, self.r) def OnClicked(self, sender, events): dialog = ColorDialog() if (dialog.ShowDialog(self) == DialogResult.OK): self.color = dialog.Color self.Invalidate() def LocateRect(self): x = (self.ClientSize.Width - RECT_WIDTH) / 2 y = (self.ClientSize.Height - RECT_HEIGHT) / 2 self.r = Rectangle(x, y, RECT_WIDTH, RECT_HEIGHT) Application.Run(IForm()) ``` 在此代碼示例中,我們使用`ColorDialog`為位于窗體控件中間的矩形選擇顏色。 ```py self.color = Color.Blue ``` 開始時,矩形的顏色是藍色。 我們使用`self.color`變量來確定矩形的顏色。 ```py dialog = ColorDialog() ``` `ColorDialog`已創建。 ```py if (dialog.ShowDialog(self) == DialogResult.OK): self.color = dialog.Color self.Invalidate() ``` 該代碼顯示顏色對話框。 如果單擊“確定”按鈕,則將獲得選定的顏色并調用`Invalidate()`方法。 該方法會使控件的整個表面無效,并使控件重畫。 結果是用新的顏色值繪制了矩形。 ![ColorDialog](https://img.kancloud.cn/00/c1/00c1e3f4c5701e287054e22160ae1d3c_450x335.jpg) 圖:`ColorDialog` ## `FontDialog` `FontDialog`用于選擇字體。 `fontdialog.py` ```py #!/usr/bin/ipy import clr clr.AddReference("System.Windows.Forms") clr.AddReference("System.Drawing") from System.Windows.Forms import Application, Form, StatusBar, Label from System.Windows.Forms import ToolBar, ToolBarButton, FontDialog from System.Windows.Forms import DialogResult class IForm(Form): def __init__(self): self.Text = "FolderBrowserDialog" self.text = Label() self.text.Parent = self self.text.Text = "Winforms tutorial" self.LocateText() toolbar = ToolBar() toolbar.Parent = self openb = ToolBarButton() toolbar.Buttons.Add(openb) toolbar.ButtonClick += self.OnClicked self.text.AutoSize = True self.Resize += self.OnResize self.CenterToScreen() def OnResize(self, sender, event): self.LocateText() def LocateText(self): self.text.Top = (self.ClientSize.Height - self.text.Height) / 2 self.text.Left = (self.ClientSize.Width - self.text.Width) / 2 def OnClicked(self, sender, event): dialog = FontDialog() if (dialog.ShowDialog(self) == DialogResult.OK): self.text.Font = dialog.Font self.LocateText() Application.Run(IForm()) ``` 我們在表單控件的中間繪制一些文本。 我們使用字體對話框更改此文本的字體。 ```py dialog = FontDialog() ``` `FontDialog`已創建。 ```py if (dialog.ShowDialog(self) == DialogResult.OK): self.text.Font = dialog.Font self.LocateText() ``` 單擊“確定”按鈕時,將為`Label`控件設置新選擇的字體。 由于文本的大小會隨著字體的變化而變化,因此我們必須調用`LocateText()`方法,該方法將文本定位在表單控件的中間。 ![FontDialog](https://img.kancloud.cn/b4/15/b415909c51a68917dbb47908ef1c6cdb_432x321.jpg) 圖:`FontDialog` ## `OpenDialog` 此對話框用于打開文件。 `opendialog.py` ```py #!/usr/bin/ipy import clr clr.AddReference("System.Windows.Forms") clr.AddReference("System.Drawing") from System.Windows.Forms import Application, Form, TextBox from System.Windows.Forms import ToolBar, ToolBarButton, OpenFileDialog from System.Windows.Forms import DialogResult, ScrollBars, DockStyle class IForm(Form): def __init__(self): self.Text = "OpenDialog" toolbar = ToolBar() toolbar.Parent = self openb = ToolBarButton() self.textbox = TextBox() self.textbox.Parent = self self.textbox.Multiline = True self.textbox.ScrollBars = ScrollBars.Both self.textbox.WordWrap = False self.textbox.Parent = self self.textbox.Dock = DockStyle.Fill toolbar.Buttons.Add(openb) toolbar.ButtonClick += self.OnClicked self.CenterToScreen() def OnClicked(self, sender, event): dialog = OpenFileDialog() dialog.Filter = "C# files (*.cs)|*.cs" if dialog.ShowDialog(self) == DialogResult.OK: f = open(dialog.FileName) data = f.read() f.Close() self.textbox.Text = data Application.Run(IForm()) ``` 我們使用`OpenDialog`控件打開 C# 源文件。 我們有一個`TextBox`控件,用于顯示文件。 ```py dialog = OpenFileDialog() ``` `OpenDialog`已創建。 ```py dialog.Filter = "C# files (*.cs)|*.cs" ``` 我們將`Filter`屬性設置為 C# 源文件。 此對話框實例只能選擇 C# 文件。 ```py if dialog.ShowDialog(self) == DialogResult.OK: f = open(dialog.FileName) data = f.read() f.Close() self.textbox.Text = data ``` 單擊確定后,我們讀取所選文件的內容并將其放入`TextBox`控件。 在 IronPython Mono Winforms 教程的這一部分中,我們展示了各種對話框。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看